PostgreSQL Source Code git master
string_utils.h File Reference
#include "libpq-fe.h"
#include "pqexpbuffer.h"
Include dependency graph for string_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

const char * fmtId (const char *rawid)
 
const char * fmtIdEnc (const char *rawid, int encoding)
 
const char * fmtQualifiedId (const char *schema, const char *id)
 
const char * fmtQualifiedIdEnc (const char *schema, const char *id, int encoding)
 
void setFmtEncoding (int encoding)
 
char * formatPGVersionNumber (int version_number, bool include_minor, char *buf, size_t buflen)
 
void appendStringLiteral (PQExpBuffer buf, const char *str, int encoding, bool std_strings)
 
void appendStringLiteralConn (PQExpBuffer buf, const char *str, PGconn *conn)
 
void appendStringLiteralDQ (PQExpBuffer buf, const char *str, const char *dqprefix)
 
void appendByteaLiteral (PQExpBuffer buf, const unsigned char *str, size_t length, bool std_strings)
 
void appendShellString (PQExpBuffer buf, const char *str)
 
bool appendShellStringNoError (PQExpBuffer buf, const char *str)
 
void appendConnStrVal (PQExpBuffer buf, const char *str)
 
void appendPsqlMetaConnect (PQExpBuffer buf, const char *dbname)
 
bool parsePGArray (const char *atext, char ***itemarray, int *nitems)
 
void appendPGArray (PQExpBuffer buffer, const char *value)
 
bool appendReloptionsArray (PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
 
bool processSQLNamePattern (PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
 
void patternToSQLRegex (int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf, PQExpBuffer namebuf, const char *pattern, bool force_escape, bool want_literal_dbname, int *dotcnt)
 

Variables

PGDLLIMPORT int quote_all_identifiers
 
PQExpBuffer(* getLocalPQExpBuffer )(void)
 

Function Documentation

◆ appendByteaLiteral()

void appendByteaLiteral ( PQExpBuffer  buf,
const unsigned char *  str,
size_t  length,
bool  std_strings 
)

Definition at line 527 of file string_utils.c.

529{
530 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 if (!enlargePQExpBuffer(buf, 2 * length + 5))
542 return;
543
544 target = buf->data + buf->len;
545 *target++ = '\'';
546 if (!std_strings)
547 *target++ = '\\';
548 *target++ = '\\';
549 *target++ = 'x';
550
551 while (length-- > 0)
552 {
553 unsigned char c = *source++;
554
555 *target++ = hextbl[(c >> 4) & 0xF];
556 *target++ = hextbl[c & 0xF];
557 }
558
559 /* Write the terminating quote and NUL character. */
560 *target++ = '\'';
561 *target = '\0';
562
563 buf->len = target - buf->data;
564}
const char * str
static rewind_source * source
Definition: pg_rewind.c:89
static char * buf
Definition: pg_test_fsync.c:72
static const char hextbl[]
Definition: pgp-info.c:87
int enlargePQExpBuffer(PQExpBuffer str, size_t needed)
Definition: pqexpbuffer.c:172
char * c

References buf, enlargePQExpBuffer(), hextbl, source, and str.

◆ appendConnStrVal()

void appendConnStrVal ( PQExpBuffer  buf,
const char *  str 
)

Definition at line 698 of file string_utils.c.

699{
700 const char *s;
701 bool needquotes;
702
703 /*
704 * If the string is one or more plain ASCII characters, no need to quote
705 * it. This is quite conservative, but better safe than sorry.
706 */
707 needquotes = true;
708 for (s = str; *s; s++)
709 {
710 if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
711 (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
712 {
713 needquotes = true;
714 break;
715 }
716 needquotes = false;
717 }
718
719 if (needquotes)
720 {
722 while (*str)
723 {
724 /* ' and \ must be escaped by to \' and \\ */
725 if (*str == '\'' || *str == '\\')
727
729 str++;
730 }
732 }
733 else
735}
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References appendPQExpBufferChar(), appendPQExpBufferStr(), buf, and str.

Referenced by appendConnStrItem(), appendPsqlMetaConnect(), constructConnStr(), generate_old_dump(), GenerateRecoveryConfig(), get_db_conn(), runPgDump(), and start_conn().

◆ appendPGArray()

void appendPGArray ( PQExpBuffer  buffer,
const char *  value 
)

Definition at line 902 of file string_utils.c.

903{
904 bool needquote;
905 const char *tmp;
906
907 if (buffer->data[buffer->len - 1] != '{')
908 appendPQExpBufferChar(buffer, ',');
909
910 /* Decide if we need quotes; this should match array_out()'s choices. */
911 if (value[0] == '\0')
912 needquote = true; /* force quotes for empty string */
913 else if (pg_strcasecmp(value, "NULL") == 0)
914 needquote = true; /* force quotes for literal NULL */
915 else
916 needquote = false;
917
918 if (!needquote)
919 {
920 for (tmp = value; *tmp; tmp++)
921 {
922 char ch = *tmp;
923
924 if (ch == '"' || ch == '\\' ||
925 ch == '{' || ch == '}' || ch == ',' ||
926 /* these match scanner_isspace(): */
927 ch == ' ' || ch == '\t' || ch == '\n' ||
928 ch == '\r' || ch == '\v' || ch == '\f')
929 {
930 needquote = true;
931 break;
932 }
933 }
934 }
935
936 if (needquote)
937 {
938 appendPQExpBufferChar(buffer, '"');
939 for (tmp = value; *tmp; tmp++)
940 {
941 char ch = *tmp;
942
943 if (ch == '"' || ch == '\\')
944 appendPQExpBufferChar(buffer, '\\');
945 appendPQExpBufferChar(buffer, ch);
946 }
947 appendPQExpBufferChar(buffer, '"');
948 }
949 else
951}
static struct @165 value
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

References appendPQExpBufferChar(), appendPQExpBufferStr(), PQExpBufferData::data, PQExpBufferData::len, pg_strcasecmp(), and value.

Referenced by getNamespaces().

◆ appendPsqlMetaConnect()

void appendPsqlMetaConnect ( PQExpBuffer  buf,
const char *  dbname 
)

Definition at line 743 of file string_utils.c.

744{
745 const char *s;
746 bool complex;
747
748 /*
749 * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
750 * For other names, even many not technically requiring it, skip to the
751 * general case. No database has a zero-length name.
752 */
753 complex = false;
754
755 for (s = dbname; *s; s++)
756 {
757 if (*s == '\n' || *s == '\r')
758 {
759 fprintf(stderr,
760 _("database name contains a newline or carriage return: \"%s\"\n"),
761 dbname);
762 exit(EXIT_FAILURE);
763 }
764
765 if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
766 (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
767 {
768 complex = true;
769 }
770 }
771
772 if (complex)
773 {
775
777
778 /*
779 * Force the target psql's encoding to SQL_ASCII. We don't really
780 * know the encoding of the database name, and it doesn't matter as
781 * long as psql will forward it to the server unchanged.
782 */
783 appendPQExpBufferStr(buf, "\\encoding SQL_ASCII\n");
784 appendPQExpBufferStr(buf, "\\connect -reuse-previous=on ");
785
786 appendPQExpBufferStr(&connstr, "dbname=");
788
789 /*
790 * As long as the name does not contain a newline, SQL identifier
791 * quoting satisfies the psql meta-command parser. Prefer not to
792 * involve psql-interpreted single quotes, which behaved differently
793 * before PostgreSQL 9.2.
794 */
796
798 }
799 else
800 {
801 appendPQExpBufferStr(buf, "\\connect ");
803 }
805}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define _(x)
Definition: elog.c:90
static char * connstr
Definition: pg_dumpall.c:88
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
#define EXIT_FAILURE
Definition: settings.h:190
char * dbname
Definition: streamutil.c:49
const char * fmtIdEnc(const char *rawid, int encoding)
Definition: string_utils.c:101
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:698

References _, appendConnStrVal(), appendPQExpBufferChar(), appendPQExpBufferStr(), buf, connstr, dbname, EXIT_FAILURE, fmtIdEnc(), fprintf, initPQExpBuffer(), PG_SQL_ASCII, and termPQExpBuffer().

Referenced by _reconnectToDB(), old_9_6_invalidate_hash_indexes(), and process_extension_updates().

◆ appendReloptionsArray()

bool appendReloptionsArray ( PQExpBuffer  buffer,
const char *  reloptions,
const char *  prefix,
int  encoding,
bool  std_strings 
)

Definition at line 966 of file string_utils.c.

968{
969 char **options;
970 int noptions;
971 int i;
972
973 if (!parsePGArray(reloptions, &options, &noptions))
974 {
975 free(options);
976 return false;
977 }
978
979 for (i = 0; i < noptions; i++)
980 {
981 char *option = options[i];
982 char *name;
983 char *separator;
984 char *value;
985
986 /*
987 * Each array element should have the form name=value. If the "=" is
988 * missing for some reason, treat it like an empty value.
989 */
990 name = option;
991 separator = strchr(option, '=');
992 if (separator)
993 {
994 *separator = '\0';
995 value = separator + 1;
996 }
997 else
998 value = "";
999
1000 if (i > 0)
1001 appendPQExpBufferStr(buffer, ", ");
1002 appendPQExpBuffer(buffer, "%s%s=", prefix, fmtId(name));
1003
1004 /*
1005 * In general we need to quote the value; but to avoid unnecessary
1006 * clutter, do not quote if it is an identifier that would not need
1007 * quoting. (We could also allow numbers, but that is a bit trickier
1008 * than it looks --- for example, are leading zeroes significant? We
1009 * don't want to assume very much here about what custom reloptions
1010 * might mean.)
1011 */
1012 if (strcmp(fmtId(value), value) == 0)
1013 appendPQExpBufferStr(buffer, value);
1014 else
1015 appendStringLiteral(buffer, value, encoding, std_strings);
1016 }
1017
1018 free(options);
1019
1020 return true;
1021}
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:74
int32 encoding
Definition: pg_database.h:41
static size_t noptions
static char ** options
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
const char * fmtId(const char *rawid)
Definition: string_utils.c:248
bool parsePGArray(const char *atext, char ***itemarray, int *nitems)
Definition: string_utils.c:819
void appendStringLiteral(PQExpBuffer buf, const char *str, int encoding, bool std_strings)
Definition: string_utils.c:351
const char * name

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteral(), encoding, fmtId(), free, i, name, noptions, options, parsePGArray(), and value.

Referenced by appendReloptionsArrayAH(), and get_create_object_cmd().

◆ appendShellString()

void appendShellString ( PQExpBuffer  buf,
const char *  str 
)

Definition at line 582 of file string_utils.c.

583{
585 {
586 fprintf(stderr,
587 _("shell command argument contains a newline or carriage return: \"%s\"\n"),
588 str);
589 exit(EXIT_FAILURE);
590 }
591}
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:594

References _, appendShellStringNoError(), buf, EXIT_FAILURE, fprintf, and str.

Referenced by cluster_conn_opts(), ensureCleanShutdown(), generate_old_dump(), getRestoreCommand(), main(), output_completion_banner(), runPgDump(), start_standby_server(), and test_specific_config_settings().

◆ appendShellStringNoError()

bool appendShellStringNoError ( PQExpBuffer  buf,
const char *  str 
)

Definition at line 594 of file string_utils.c.

595{
596#ifdef WIN32
597 int backslash_run_length = 0;
598#endif
599 bool ok = true;
600 const char *p;
601
602 /*
603 * Don't bother with adding quotes if the string is nonempty and clearly
604 * contains only safe characters.
605 */
606 if (*str != '\0' &&
607 strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_./:") == strlen(str))
608 {
610 return ok;
611 }
612
613#ifndef WIN32
615 for (p = str; *p; p++)
616 {
617 if (*p == '\n' || *p == '\r')
618 {
619 ok = false;
620 continue;
621 }
622
623 if (*p == '\'')
624 appendPQExpBufferStr(buf, "'\"'\"'");
625 else
627 }
629#else /* WIN32 */
630
631 /*
632 * A Windows system() argument experiences two layers of interpretation.
633 * First, cmd.exe interprets the string. Its behavior is undocumented,
634 * but a caret escapes any byte except LF or CR that would otherwise have
635 * special meaning. Handling of a caret before LF or CR differs between
636 * "cmd.exe /c" and other modes, and it is unusable here.
637 *
638 * Second, the new process parses its command line to construct argv (see
639 * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx). This treats
640 * backslash-double quote sequences specially.
641 */
643 for (p = str; *p; p++)
644 {
645 if (*p == '\n' || *p == '\r')
646 {
647 ok = false;
648 continue;
649 }
650
651 /* Change N backslashes before a double quote to 2N+1 backslashes. */
652 if (*p == '"')
653 {
654 while (backslash_run_length)
655 {
657 backslash_run_length--;
658 }
660 }
661 else if (*p == '\\')
662 backslash_run_length++;
663 else
664 backslash_run_length = 0;
665
666 /*
667 * Decline to caret-escape the most mundane characters, to ease
668 * debugging and lest we approach the command length limit.
669 */
670 if (!((*p >= 'a' && *p <= 'z') ||
671 (*p >= 'A' && *p <= 'Z') ||
672 (*p >= '0' && *p <= '9')))
675 }
676
677 /*
678 * Change N backslashes at end of argument to 2N backslashes, because they
679 * precede the double quote that terminates the argument.
680 */
681 while (backslash_run_length)
682 {
684 backslash_run_length--;
685 }
687#endif /* WIN32 */
688
689 return ok;
690}

References appendPQExpBufferChar(), appendPQExpBufferStr(), buf, and str.

Referenced by appendShellString(), and psql_get_variable().

◆ appendStringLiteral()

void appendStringLiteral ( PQExpBuffer  buf,
const char *  str,
int  encoding,
bool  std_strings 
)

Definition at line 351 of file string_utils.c.

353{
354 size_t length = strlen(str);
355 const char *source = str;
356 char *target;
357 size_t remaining = length;
358
359 if (!enlargePQExpBuffer(buf, 2 * length + 2))
360 return;
361
362 target = buf->data + buf->len;
363 *target++ = '\'';
364
365 while (remaining > 0)
366 {
367 char c = *source;
368 int charlen;
369 int i;
370
371 /* Fast path for plain ASCII */
372 if (!IS_HIGHBIT_SET(c))
373 {
374 /* Apply quoting if needed */
375 if (SQL_STR_DOUBLE(c, !std_strings))
376 *target++ = c;
377 /* Copy the character */
378 *target++ = c;
379 source++;
380 remaining--;
381 continue;
382 }
383
384 /* Slow path for possible multibyte characters */
385 charlen = PQmblen(source, encoding);
386
387 if (remaining < charlen ||
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 */
411 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 source++;
419 remaining--;
420 }
421 else
422 {
423 /* Copy the character */
424 for (i = 0; i < charlen; i++)
425 {
426 *target++ = *source++;
427 remaining--;
428 }
429 }
430 }
431
432 /* Write the terminating quote and NUL character. */
433 *target++ = '\'';
434 *target = '\0';
435
436 buf->len = target - buf->data;
437}
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1126
#define SQL_STR_DOUBLE(ch, escape_backslash)
Definition: c.h:1134
int PQmblen(const char *s, int encoding)
Definition: fe-misc.c:1233
int remaining
Definition: informix.c:692
void pg_encoding_set_invalid(int encoding, char *dst)
Definition: wchar.c:2049
int pg_encoding_verifymbchar(int encoding, const char *mbstr, int len)
Definition: wchar.c:2150

References buf, encoding, enlargePQExpBuffer(), i, IS_HIGHBIT_SET, pg_encoding_set_invalid(), pg_encoding_verifymbchar(), PQmblen(), remaining, source, SQL_STR_DOUBLE, and str.

Referenced by appendReloptionsArray(), appendStringLiteralConn(), and escape_append_literal().

◆ appendStringLiteralConn()

void appendStringLiteralConn ( PQExpBuffer  buf,
const char *  str,
PGconn conn 
)

Definition at line 446 of file string_utils.c.

447{
448 size_t length = strlen(str);
449
450 /*
451 * XXX This is a kluge to silence escape_string_warning in our utility
452 * programs. It should go away someday.
453 */
454 if (strchr(str, '\\') != NULL && PQserverVersion(conn) >= 80100)
455 {
456 /* ensure we are not adjacent to an identifier */
457 if (buf->len > 0 && buf->data[buf->len - 1] != ' ')
461 return;
462 }
463 /* XXX end kluge */
464
465 if (!enlargePQExpBuffer(buf, 2 * length + 2))
466 return;
468 buf->len += PQescapeStringConn(conn, buf->data + buf->len,
469 str, length, NULL);
471}
#define ESCAPE_STRING_SYNTAX
Definition: c.h:1137
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7543
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7643
size_t PQescapeStringConn(PGconn *conn, char *to, const char *from, size_t length, int *error)
Definition: fe-exec.c:4176
PGconn * conn
Definition: streamutil.c:52

References appendPQExpBufferChar(), appendStringLiteral(), buf, conn, enlargePQExpBuffer(), ESCAPE_STRING_SYNTAX, PQclientEncoding(), PQescapeStringConn(), PQserverVersion(), and str.

Referenced by append_db_pattern_cte(), append_rel_pattern_filtered_cte(), append_rel_pattern_raw_cte(), appendQualifiedRelation(), create_logical_replication_slots(), dumpRoles(), dumpTablespaces(), dumpUserConfig(), emitShSecLabels(), get_parallel_tables_list(), getTables(), lookup_object_oid(), main(), makeAlterConfigCommand(), processSQLNamePattern(), retrieve_objects(), and setup_connection().

◆ appendStringLiteralDQ()

void appendStringLiteralDQ ( PQExpBuffer  buf,
const char *  str,
const char *  dqprefix 
)

Definition at line 484 of file string_utils.c.

485{
486 static const char suffixes[] = "_XXXXXXX";
487 int nextchar = 0;
488 PQExpBuffer delimBuf = createPQExpBuffer();
489
490 /* start with $ + dqprefix if not NULL */
491 appendPQExpBufferChar(delimBuf, '$');
492 if (dqprefix)
493 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 while (strstr(str, delimBuf->data) != NULL)
501 {
502 appendPQExpBufferChar(delimBuf, suffixes[nextchar++]);
503 nextchar %= sizeof(suffixes) - 1;
504 }
505
506 /* add trailing $ */
507 appendPQExpBufferChar(delimBuf, '$');
508
509 /* quote it and we are all done */
510 appendPQExpBufferStr(buf, delimBuf->data);
512 appendPQExpBufferStr(buf, delimBuf->data);
513
514 destroyPQExpBuffer(delimBuf);
515}
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114

References appendPQExpBufferChar(), appendPQExpBufferStr(), buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), and str.

Referenced by dumpFunc().

◆ fmtId()

const char * fmtId ( const char *  rawid)

Definition at line 248 of file string_utils.c.

249{
250 return fmtIdEnc(rawid, getFmtEncoding());
251}
static int getFmtEncoding(void)
Definition: string_utils.c:78

References fmtIdEnc(), and getFmtEncoding().

Referenced by _doSetFixedOutputState(), _getObjectDescription(), _printTableAccessMethodNoStorage(), _printTocEntry(), _selectOutputSchema(), _selectTableAccessMethod(), _selectTablespace(), append_depends_on_extension(), appendReloptionsArray(), binary_upgrade_extension_member(), buildACLCommands(), buildDefaultACLCommands(), createDummyViewAsClause(), describeOneTableDetails(), dropDBs(), dropRoles(), dropTablespaces(), dumpAccessMethod(), dumpAgg(), dumpAttrDef(), dumpBaseType(), dumpCast(), dumpCollation(), dumpCommentExtended(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpConstraint(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpFunc(), dumpIndex(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpPublicationNamespace(), dumpPublicationTable(), dumpRangeType(), dumpRoleGUCPrivs(), dumpRoleMembership(), dumpRoles(), dumpRule(), dumpSearchPath(), dumpSecLabel(), dumpSequence(), dumpStatisticsExt(), dumpSubscription(), dumpTable(), dumpTableComment(), dumpTableConstraintComment(), dumpTableData_insert(), dumpTableSchema(), dumpTableSecLabel(), dumpTablespaces(), dumpTransform(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), dumpUserMappings(), emitShSecLabels(), escape_fmt_id(), fmtCopyColumnList(), format_aggregate_signature(), format_function_arguments(), format_function_signature(), get_create_object_cmd(), get_language_name(), getFormattedOperatorName(), getPublicationTables(), main(), makeAlterConfigCommand(), and setup_connection().

◆ fmtIdEnc()

const char * fmtIdEnc ( const char *  rawid,
int  encoding 
)

Definition at line 101 of file string_utils.c.

102{
103 PQExpBuffer id_return = getLocalPQExpBuffer();
104
105 const char *cp;
106 bool need_quotes = false;
107 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 */
114 need_quotes = true;
115 /* slightly different rules for first character */
116 else if (!((rawid[0] >= 'a' && rawid[0] <= 'z') || rawid[0] == '_'))
117 need_quotes = true;
118 else
119 {
120 /* otherwise check the entire string */
121 cp = rawid;
122 for (size_t i = 0; i < remaining; i++, cp++)
123 {
124 if (!((*cp >= 'a' && *cp <= 'z')
125 || (*cp >= '0' && *cp <= '9')
126 || (*cp == '_')))
127 {
128 need_quotes = true;
129 break;
130 }
131 }
132 }
133
134 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 int kwnum = ScanKeywordLookup(rawid, &ScanKeywords);
145
146 if (kwnum >= 0 && ScanKeywordCategories[kwnum] != UNRESERVED_KEYWORD)
147 need_quotes = true;
148 }
149
150 if (!need_quotes)
151 {
152 /* no quoting needed */
153 appendPQExpBufferStr(id_return, rawid);
154 }
155 else
156 {
157 appendPQExpBufferChar(id_return, '"');
158
159 cp = &rawid[0];
160 while (remaining > 0)
161 {
162 int charlen;
163
164 /* Fast path for plain ASCII */
165 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 if (*cp == '"')
173 appendPQExpBufferChar(id_return, '"');
174 appendPQExpBufferChar(id_return, *cp);
175 remaining--;
176 cp++;
177 continue;
178 }
179
180 /* Slow path for possible multibyte characters */
181 charlen = pg_encoding_mblen(encoding, cp);
182
183 if (remaining < charlen ||
184 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 if (enlargePQExpBuffer(id_return, 2))
204 {
206 id_return->data + id_return->len);
207 id_return->len += 2;
208 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 remaining--;
218 cp++;
219 }
220 else
221 {
222 for (int i = 0; i < charlen; i++)
223 {
224 appendPQExpBufferChar(id_return, *cp);
225 remaining--;
226 cp++;
227 }
228 }
229 }
230
231 appendPQExpBufferChar(id_return, '"');
232 }
233
234 return id_return->data;
235}
const uint8 ScanKeywordCategories[SCANKEYWORDS_NUM_KEYWORDS]
Definition: keywords.c:29
PGDLLIMPORT const ScanKeywordList ScanKeywords
#define UNRESERVED_KEYWORD
Definition: keywords.h:20
int ScanKeywordLookup(const char *str, const ScanKeywordList *keywords)
Definition: kwlookup.c:38
PQExpBuffer(* getLocalPQExpBuffer)(void)
Definition: string_utils.c:28
int quote_all_identifiers
Definition: string_utils.c:27
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2116

References appendPQExpBufferChar(), appendPQExpBufferStr(), PQExpBufferData::data, encoding, enlargePQExpBuffer(), getLocalPQExpBuffer, i, IS_HIGHBIT_SET, PQExpBufferData::len, pg_encoding_mblen(), pg_encoding_set_invalid(), pg_encoding_verifymbchar(), quote_all_identifiers, remaining, ScanKeywordCategories, ScanKeywordLookup(), ScanKeywords, and UNRESERVED_KEYWORD.

Referenced by appendPsqlMetaConnect(), fmtId(), fmtQualifiedIdEnc(), gen_reindex_command(), and main().

◆ fmtQualifiedId()

const char * fmtQualifiedId ( const char *  schema,
const char *  id 
)

Definition at line 296 of file string_utils.c.

297{
298 return fmtQualifiedIdEnc(schema, id, getFmtEncoding());
299}
const char * fmtQualifiedIdEnc(const char *schema, const char *id, int encoding)
Definition: string_utils.c:263

References fmtQualifiedIdEnc(), and getFmtEncoding().

Referenced by _disableTriggersIfNecessary(), _enableTriggersIfNecessary(), _printTableAccessMethodNoStorage(), is_load_via_partition_root(), lockTableForWorker(), and restore_toc_entry().

◆ fmtQualifiedIdEnc()

const char * fmtQualifiedIdEnc ( const char *  schema,
const char *  id,
int  encoding 
)

Definition at line 263 of file string_utils.c.

264{
265 PQExpBuffer id_return;
266 PQExpBuffer lcl_pqexp = createPQExpBuffer();
267
268 /* Some callers might fail to provide a schema name */
269 if (schema && *schema)
270 {
271 appendPQExpBuffer(lcl_pqexp, "%s.", fmtIdEnc(schema, encoding));
272 }
273 appendPQExpBufferStr(lcl_pqexp, fmtIdEnc(id, encoding));
274
275 id_return = getLocalPQExpBuffer();
276
277 appendPQExpBufferStr(id_return, lcl_pqexp->data);
278 destroyPQExpBuffer(lcl_pqexp);
279
280 return id_return->data;
281}

References appendPQExpBuffer(), appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), encoding, fmtIdEnc(), and getLocalPQExpBuffer.

Referenced by appendQualifiedRelation(), fmtQualifiedId(), get_parallel_tabidx_list(), get_parallel_tables_list(), and retrieve_objects().

◆ formatPGVersionNumber()

char * formatPGVersionNumber ( int  version_number,
bool  include_minor,
char *  buf,
size_t  buflen 
)

Definition at line 313 of file string_utils.c.

315{
316 if (version_number >= 100000)
317 {
318 /* New two-part style */
319 if (include_minor)
320 snprintf(buf, buflen, "%d.%d", version_number / 10000,
321 version_number % 10000);
322 else
323 snprintf(buf, buflen, "%d", version_number / 10000);
324 }
325 else
326 {
327 /* Old three-part style */
328 if (include_minor)
329 snprintf(buf, buflen, "%d.%d.%d", version_number / 10000,
330 (version_number / 100) % 100,
331 version_number % 100);
332 else
333 snprintf(buf, buflen, "%d.%d", version_number / 10000,
334 (version_number / 100) % 100);
335 }
336 return buf;
337}
#define snprintf
Definition: port.h:239

References buf, and snprintf.

Referenced by connection_warnings(), describeAccessMethods(), describeFunctions(), describePublications(), describeSubscriptions(), listEventTriggers(), listExtendedStats(), listPartitionedTables(), listPublications(), printVersion(), and SyncVariables().

◆ parsePGArray()

bool parsePGArray ( const char *  atext,
char ***  itemarray,
int *  nitems 
)

Definition at line 819 of file string_utils.c.

820{
821 int inputlen;
822 char **items;
823 char *strings;
824 int curitem;
825
826 /*
827 * We expect input in the form of "{item,item,item}" where any item is
828 * either raw data, or surrounded by double quotes (in which case embedded
829 * characters including backslashes and quotes are backslashed).
830 *
831 * We build the result as an array of pointers followed by the actual
832 * string data, all in one malloc block for convenience of deallocation.
833 * The worst-case storage need is not more than one pointer and one
834 * character for each input character (consider "{,,,,,,,,,,}").
835 */
836 *itemarray = NULL;
837 *nitems = 0;
838 inputlen = strlen(atext);
839 if (inputlen < 2 || atext[0] != '{' || atext[inputlen - 1] != '}')
840 return false; /* bad input */
841 items = (char **) malloc(inputlen * (sizeof(char *) + sizeof(char)));
842 if (items == NULL)
843 return false; /* out of memory */
844 *itemarray = items;
845 strings = (char *) (items + inputlen);
846
847 atext++; /* advance over initial '{' */
848 curitem = 0;
849 while (*atext != '}')
850 {
851 if (*atext == '\0')
852 return false; /* premature end of string */
853 items[curitem] = strings;
854 while (*atext != '}' && *atext != ',')
855 {
856 if (*atext == '\0')
857 return false; /* premature end of string */
858 if (*atext != '"')
859 *strings++ = *atext++; /* copy unquoted data */
860 else
861 {
862 /* process quoted substring */
863 atext++;
864 while (*atext != '"')
865 {
866 if (*atext == '\0')
867 return false; /* premature end of string */
868 if (*atext == '\\')
869 {
870 atext++;
871 if (*atext == '\0')
872 return false; /* premature end of string */
873 }
874 *strings++ = *atext++; /* copy quoted data */
875 }
876 atext++;
877 }
878 }
879 *strings++ = '\0';
880 if (*atext == ',')
881 atext++;
882 curitem++;
883 }
884 if (atext[1] != '\0')
885 return false; /* bogus syntax (embedded '}') */
886 *nitems = curitem;
887 return true;
888}
#define malloc(a)
Definition: header.h:50
#define nitems(x)
Definition: indent.h:31
static ItemArray items
Definition: test_tidstore.c:48

References items, malloc, and nitems.

Referenced by appendReloptionsArray(), buildACLCommands(), dumpFunc(), dumpIndex(), dumpSearchPath(), dumpSubscription(), getIndexes(), getPublicationTables(), and processExtensionTables().

◆ patternToSQLRegex()

void patternToSQLRegex ( int  encoding,
PQExpBuffer  dbnamebuf,
PQExpBuffer  schemabuf,
PQExpBuffer  namebuf,
const char *  pattern,
bool  force_escape,
bool  want_literal_dbname,
int *  dotcnt 
)

Definition at line 1225 of file string_utils.c.

1228{
1230 PQExpBufferData left_literal;
1231 PQExpBuffer curbuf;
1232 PQExpBuffer maxbuf;
1233 int i;
1234 bool inquotes;
1235 bool left;
1236 const char *cp;
1237
1238 Assert(pattern != NULL);
1239 Assert(namebuf != NULL);
1240
1241 /* callers should never expect "dbname.relname" format */
1242 Assert(dbnamebuf == NULL || schemabuf != NULL);
1243 Assert(dotcnt != NULL);
1244
1245 *dotcnt = 0;
1246 inquotes = false;
1247 cp = pattern;
1248
1249 if (dbnamebuf != NULL)
1250 maxbuf = &buf[2];
1251 else if (schemabuf != NULL)
1252 maxbuf = &buf[1];
1253 else
1254 maxbuf = &buf[0];
1255
1256 curbuf = &buf[0];
1257 if (want_literal_dbname)
1258 {
1259 left = true;
1260 initPQExpBuffer(&left_literal);
1261 }
1262 else
1263 left = false;
1264 initPQExpBuffer(curbuf);
1265 appendPQExpBufferStr(curbuf, "^(");
1266 while (*cp)
1267 {
1268 char ch = *cp;
1269
1270 if (ch == '"')
1271 {
1272 if (inquotes && cp[1] == '"')
1273 {
1274 /* emit one quote, stay in inquotes mode */
1275 appendPQExpBufferChar(curbuf, '"');
1276 if (left)
1277 appendPQExpBufferChar(&left_literal, '"');
1278 cp++;
1279 }
1280 else
1281 inquotes = !inquotes;
1282 cp++;
1283 }
1284 else if (!inquotes && isupper((unsigned char) ch))
1285 {
1286 appendPQExpBufferChar(curbuf,
1287 pg_tolower((unsigned char) ch));
1288 if (left)
1289 appendPQExpBufferChar(&left_literal,
1290 pg_tolower((unsigned char) ch));
1291 cp++;
1292 }
1293 else if (!inquotes && ch == '*')
1294 {
1295 appendPQExpBufferStr(curbuf, ".*");
1296 if (left)
1297 appendPQExpBufferChar(&left_literal, '*');
1298 cp++;
1299 }
1300 else if (!inquotes && ch == '?')
1301 {
1302 appendPQExpBufferChar(curbuf, '.');
1303 if (left)
1304 appendPQExpBufferChar(&left_literal, '?');
1305 cp++;
1306 }
1307 else if (!inquotes && ch == '.')
1308 {
1309 left = false;
1310 if (dotcnt)
1311 (*dotcnt)++;
1312 if (curbuf < maxbuf)
1313 {
1314 appendPQExpBufferStr(curbuf, ")$");
1315 curbuf++;
1316 initPQExpBuffer(curbuf);
1317 appendPQExpBufferStr(curbuf, "^(");
1318 cp++;
1319 }
1320 else
1321 appendPQExpBufferChar(curbuf, *cp++);
1322 }
1323 else if (ch == '$')
1324 {
1325 /*
1326 * Dollar is always quoted, whether inside quotes or not. The
1327 * reason is that it's allowed in SQL identifiers, so there's a
1328 * significant use-case for treating it literally, while because
1329 * we anchor the pattern automatically there is no use-case for
1330 * having it possess its regexp meaning.
1331 */
1332 appendPQExpBufferStr(curbuf, "\\$");
1333 if (left)
1334 appendPQExpBufferChar(&left_literal, '$');
1335 cp++;
1336 }
1337 else
1338 {
1339 /*
1340 * Ordinary data character, transfer to pattern
1341 *
1342 * Inside double quotes, or at all times if force_escape is true,
1343 * quote regexp special characters with a backslash to avoid
1344 * regexp errors. Outside quotes, however, let them pass through
1345 * as-is; this lets knowledgeable users build regexp expressions
1346 * that are more powerful than shell-style patterns.
1347 *
1348 * As an exception to that, though, always quote "[]", as that's
1349 * much more likely to be an attempt to write an array type name
1350 * than it is to be the start of a regexp bracket expression.
1351 */
1352 if ((inquotes || force_escape) &&
1353 strchr("|*+?()[]{}.^$\\", ch))
1354 appendPQExpBufferChar(curbuf, '\\');
1355 else if (ch == '[' && cp[1] == ']')
1356 appendPQExpBufferChar(curbuf, '\\');
1357 i = PQmblenBounded(cp, encoding);
1358 while (i--)
1359 {
1360 if (left)
1361 appendPQExpBufferChar(&left_literal, *cp);
1362 appendPQExpBufferChar(curbuf, *cp++);
1363 }
1364 }
1365 }
1366 appendPQExpBufferStr(curbuf, ")$");
1367
1368 if (namebuf)
1369 {
1370 appendPQExpBufferStr(namebuf, curbuf->data);
1371 termPQExpBuffer(curbuf);
1372 curbuf--;
1373 }
1374
1375 if (schemabuf && curbuf >= buf)
1376 {
1377 appendPQExpBufferStr(schemabuf, curbuf->data);
1378 termPQExpBuffer(curbuf);
1379 curbuf--;
1380 }
1381
1382 if (dbnamebuf && curbuf >= buf)
1383 {
1384 if (want_literal_dbname)
1385 appendPQExpBufferStr(dbnamebuf, left_literal.data);
1386 else
1387 appendPQExpBufferStr(dbnamebuf, curbuf->data);
1388 termPQExpBuffer(curbuf);
1389 }
1390
1391 if (want_literal_dbname)
1392 termPQExpBuffer(&left_literal);
1393}
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1243
Assert(PointerIsAligned(start, uint64))
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122

References appendPQExpBufferChar(), appendPQExpBufferStr(), Assert(), buf, PQExpBufferData::data, encoding, i, initPQExpBuffer(), pg_tolower(), PQmblenBounded(), and termPQExpBuffer().

Referenced by append_database_pattern(), append_relation_pattern_helper(), append_schema_pattern(), and processSQLNamePattern().

◆ processSQLNamePattern()

bool processSQLNamePattern ( PGconn conn,
PQExpBuffer  buf,
const char *  pattern,
bool  have_where,
bool  force_escape,
const char *  schemavar,
const char *  namevar,
const char *  altnamevar,
const char *  visibilityrule,
PQExpBuffer  dbnamebuf,
int *  dotcnt 
)

Definition at line 1053 of file string_utils.c.

1058{
1059 PQExpBufferData schemabuf;
1060 PQExpBufferData namebuf;
1061 bool added_clause = false;
1062 int dcnt;
1063
1064#define WHEREAND() \
1065 (appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \
1066 have_where = true, added_clause = true)
1067
1068 if (dotcnt == NULL)
1069 dotcnt = &dcnt;
1070 *dotcnt = 0;
1071 if (pattern == NULL)
1072 {
1073 /* Default: select all visible objects */
1074 if (visibilityrule)
1075 {
1076 WHEREAND();
1077 appendPQExpBuffer(buf, "%s\n", visibilityrule);
1078 }
1079 return added_clause;
1080 }
1081
1082 initPQExpBuffer(&schemabuf);
1083 initPQExpBuffer(&namebuf);
1084
1085 /*
1086 * Convert shell-style 'pattern' into the regular expression(s) we want to
1087 * execute. Quoting/escaping into SQL literal format will be done below
1088 * using appendStringLiteralConn().
1089 *
1090 * If the caller provided a schemavar, we want to split the pattern on
1091 * ".", otherwise not.
1092 */
1094 (schemavar ? dbnamebuf : NULL),
1095 (schemavar ? &schemabuf : NULL),
1096 &namebuf,
1097 pattern, force_escape, true, dotcnt);
1098
1099 /*
1100 * Now decide what we need to emit. We may run under a hostile
1101 * search_path, so qualify EVERY name. Note there will be a leading "^("
1102 * in the patterns in any case.
1103 *
1104 * We want the regex matches to use the database's default collation where
1105 * collation-sensitive behavior is required (for example, which characters
1106 * match '\w'). That happened by default before PG v12, but if the server
1107 * is >= v12 then we need to force it through explicit COLLATE clauses,
1108 * otherwise the "C" collation attached to "name" catalog columns wins.
1109 */
1110 if (namevar && namebuf.len > 2)
1111 {
1112 /* We have a name pattern, so constrain the namevar(s) */
1113
1114 /* Optimize away a "*" pattern */
1115 if (strcmp(namebuf.data, "^(.*)$") != 0)
1116 {
1117 WHEREAND();
1118 if (altnamevar)
1119 {
1121 "(%s OPERATOR(pg_catalog.~) ", namevar);
1123 if (PQserverVersion(conn) >= 120000)
1124 appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1126 "\n OR %s OPERATOR(pg_catalog.~) ",
1127 altnamevar);
1129 if (PQserverVersion(conn) >= 120000)
1130 appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1131 appendPQExpBufferStr(buf, ")\n");
1132 }
1133 else
1134 {
1135 appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", namevar);
1137 if (PQserverVersion(conn) >= 120000)
1138 appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1140 }
1141 }
1142 }
1143
1144 if (schemavar && schemabuf.len > 2)
1145 {
1146 /* We have a schema pattern, so constrain the schemavar */
1147
1148 /* Optimize away a "*" pattern */
1149 if (strcmp(schemabuf.data, "^(.*)$") != 0 && schemavar)
1150 {
1151 WHEREAND();
1152 appendPQExpBuffer(buf, "%s OPERATOR(pg_catalog.~) ", schemavar);
1153 appendStringLiteralConn(buf, schemabuf.data, conn);
1154 if (PQserverVersion(conn) >= 120000)
1155 appendPQExpBufferStr(buf, " COLLATE pg_catalog.default");
1157 }
1158 }
1159 else
1160 {
1161 /* No schema pattern given, so select only visible objects */
1162 if (visibilityrule)
1163 {
1164 WHEREAND();
1165 appendPQExpBuffer(buf, "%s\n", visibilityrule);
1166 }
1167 }
1168
1169 termPQExpBuffer(&schemabuf);
1170 termPQExpBuffer(&namebuf);
1171
1172 return added_clause;
1173#undef WHEREAND
1174}
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446
void patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf, PQExpBuffer namebuf, const char *pattern, bool force_escape, bool want_literal_dbname, int *dotcnt)
#define WHEREAND()

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendStringLiteralConn(), buf, conn, PQExpBufferData::data, initPQExpBuffer(), PQExpBufferData::len, patternToSQLRegex(), PQclientEncoding(), PQserverVersion(), termPQExpBuffer(), and WHEREAND.

Referenced by describeConfigurationParameters(), expand_dbname_patterns(), expand_extension_name_patterns(), expand_foreign_server_name_patterns(), expand_schema_name_patterns(), expand_table_name_patterns(), and validateSQLNamePattern().

◆ setFmtEncoding()

void setFmtEncoding ( int  encoding)

Definition at line 69 of file string_utils.c.

70{
71 fmtIdEncoding = encoding;
72}

References encoding.

Referenced by escape_fmt_id(), exec_command_encoding(), main(), processEncodingEntry(), setup_connection(), and SyncVariables().

Variable Documentation

◆ getLocalPQExpBuffer

PQExpBuffer(* getLocalPQExpBuffer) (void) ( void  )
externdefault

Definition at line 28 of file string_utils.c.

43{
44 static PQExpBuffer id_return = NULL;
45
46 if (id_return) /* first time through? */
47 {
48 /* same buffer, just wipe contents */
49 resetPQExpBuffer(id_return);
50 }
51 else
52 {
53 /* new buffer */
54 id_return = createPQExpBuffer();
55 }
56
57 return id_return;
58}
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146

Referenced by fmtIdEnc(), fmtQualifiedIdEnc(), and ParallelBackupStart().

◆ quote_all_identifiers

PGDLLIMPORT int quote_all_identifiers
extern

Definition at line 339 of file ruleutils.c.