MCPcopy Create free account
hub / github.com/apache/cloudberry / appendPsqlMetaConnect

Function appendPsqlMetaConnect

src/fe_utils/string_utils.c:763–826  ·  view source on GitHub ↗

* Append a psql meta-command that connects to the given database with the * then-current connection's user, host and port. */

Source from the content-addressed store, hash-verified

761 * then-current connection's user, host and port.
762 */
763void
764appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
765{
766 const char *s;
767 bool complex;
768
769 /*
770 * If the name is plain ASCII characters, emit a trivial "\connect "foo"".
771 * For other names, even many not technically requiring it, skip to the
772 * general case. No database has a zero-length name.
773 */
774 complex = false;
775
776 for (s = dbname; *s; s++)
777 {
778 if (*s == '\n' || *s == '\r')
779 {
780 fprintf(stderr,
781 _("database name contains a newline or carriage return: \"%s\"\n"),
782 dbname);
783 exit(EXIT_FAILURE);
784 }
785
786 if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
787 (*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
788 {
789 complex = true;
790 }
791 }
792
793 if (complex)
794 {
795 PQExpBufferData connstr;
796
797 initPQExpBuffer(&connstr);
798
799 /*
800 * Force the target psql's encoding to SQL_ASCII. We don't really
801 * know the encoding of the database name, and it doesn't matter as
802 * long as psql will forward it to the server unchanged.
803 */
804 appendPQExpBufferStr(buf, "\\encoding SQL_ASCII\n");
805 appendPQExpBufferStr(buf, "\\connect -reuse-previous=on ");
806
807 appendPQExpBufferStr(&connstr, "dbname=");
808 appendConnStrVal(&connstr, dbname);
809
810 /*
811 * As long as the name does not contain a newline, SQL identifier
812 * quoting satisfies the psql meta-command parser. Prefer not to
813 * involve psql-interpreted single quotes, which behaved differently
814 * before PostgreSQL 9.2.
815 */
816 appendPQExpBufferStr(buf, fmtIdEnc(connstr.data, PG_SQL_ASCII));
817
818 termPQExpBuffer(&connstr);
819 }
820 else

Callers

nothing calls this directly

Calls 6

initPQExpBufferFunction · 0.85
appendPQExpBufferStrFunction · 0.85
fmtIdEncFunction · 0.85
termPQExpBufferFunction · 0.85
appendPQExpBufferCharFunction · 0.85
appendConnStrValFunction · 0.70

Tested by

no test coverage detected