2016-03-24 15:55:44 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* String-processing utility routines for frontend code
|
|
|
|
|
*
|
2016-08-08 10:07:46 -04:00
|
|
|
* Utility functions that interpret backend output or quote strings for
|
|
|
|
|
* assorted contexts.
|
2016-03-24 15:55:44 -04:00
|
|
|
*
|
|
|
|
|
*
|
2022-01-07 19:04:57 -05:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2016-03-24 15:55:44 -04:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
|
*
|
|
|
|
|
* src/include/fe_utils/string_utils.h
|
|
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
#ifndef STRING_UTILS_H
|
|
|
|
|
#define STRING_UTILS_H
|
|
|
|
|
|
|
|
|
|
#include "libpq-fe.h"
|
|
|
|
|
#include "pqexpbuffer.h"
|
|
|
|
|
|
|
|
|
|
/* Global variables controlling behavior of fmtId() and fmtQualifiedId() */
|
2022-04-08 08:16:38 -04:00
|
|
|
extern PGDLLIMPORT int quote_all_identifiers;
|
2016-03-24 15:55:44 -04:00
|
|
|
extern PQExpBuffer (*getLocalPQExpBuffer) (void);
|
|
|
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
extern const char *fmtId(const char *identifier);
|
Ensure schema qualification in pg_restore DISABLE/ENABLE TRIGGER commands.
Previously, this code blindly followed the common coding pattern of
passing PQserverVersion(AH->connection) as the server-version parameter
of fmtQualifiedId. That works as long as we have a connection; but in
pg_restore with text output, we don't. Instead we got a zero from
PQserverVersion, which fmtQualifiedId interpreted as "server is too old to
have schemas", and so the name went unqualified. That still accidentally
managed to work in many cases, which is probably why this ancient bug went
undetected for so long. It only became obvious in the wake of the changes
to force dump/restore to execute with restricted search_path.
In HEAD/v11, let's deal with this by ripping out fmtQualifiedId's server-
version behavioral dependency, and just making it schema-qualify all the
time. We no longer support pg_dump from servers old enough to need the
ability to omit schema name, let alone restoring to them. (Also, the few
callers outside pg_dump already didn't work with pre-schema servers.)
In older branches, that's not an acceptable solution, so instead just
tweak the DISABLE/ENABLE TRIGGER logic to ensure it will schema-qualify
its output regardless of server version.
Per bug #15338 from Oleg somebody. Back-patch to all supported branches.
Discussion: https://postgr.es/m/153452458706.1316.5328079417086507743@wrigleys.postgresql.org
2018-08-17 17:12:21 -04:00
|
|
|
extern const char *fmtQualifiedId(const char *schema, const char *id);
|
2016-03-24 15:55:44 -04:00
|
|
|
|
Fix assorted places in psql to print version numbers >= 10 in new style.
This is somewhat cosmetic, since as long as you know what you are looking
at, "10.0" is a serviceable substitute for "10". But there is a potential
for confusion between version numbers with minor numbers and those without
--- we don't want people asking "why is psql saying 10.0 when my server is
10.2". Therefore, back-patch as far as practical, which turns out to be
9.3. I could have redone the patch to use fprintf(stderr) in place of
psql_error(), but it seems more work than is warranted for branches that
will be EOL or nearly so by the time v10 comes out.
Although only psql seems to contain any code that needs this, I chose
to put the support function into fe_utils, since it seems likely we'll
need it in other client programs in future. (In 9.3-9.5, use dumputils.c,
the predecessor of fe_utils/string_utils.c.)
In HEAD, also fix the backend code that whines about loadable-library
version mismatch. I don't see much need to back-patch that.
2016-08-16 15:58:30 -04:00
|
|
|
extern char *formatPGVersionNumber(int version_number, bool include_minor,
|
|
|
|
|
char *buf, size_t buflen);
|
|
|
|
|
|
2016-03-24 15:55:44 -04:00
|
|
|
extern void appendStringLiteral(PQExpBuffer buf, const char *str,
|
|
|
|
|
int encoding, bool std_strings);
|
|
|
|
|
extern void appendStringLiteralConn(PQExpBuffer buf, const char *str,
|
|
|
|
|
PGconn *conn);
|
|
|
|
|
extern void appendStringLiteralDQ(PQExpBuffer buf, const char *str,
|
|
|
|
|
const char *dqprefix);
|
|
|
|
|
extern void appendByteaLiteral(PQExpBuffer buf,
|
|
|
|
|
const unsigned char *str, size_t length,
|
|
|
|
|
bool std_strings);
|
|
|
|
|
|
2016-08-08 10:07:46 -04:00
|
|
|
extern void appendShellString(PQExpBuffer buf, const char *str);
|
Allow psql variable substitution to occur in backtick command strings.
Previously, text between backquotes in a psql metacommand's arguments
was always passed to the shell literally. That considerably hobbles
the usefulness of the feature for scripting, so we'd foreseen for a long
time that we'd someday want to allow substitution of psql variables into
the shell command. IMO the addition of \if metacommands has brought us to
that point, since \if can greatly benefit from some sort of client-side
expression evaluation capability, and psql itself is not going to grow any
such thing in time for v10. Hence, this patch. It allows :VARIABLE to be
replaced by the exact contents of the named variable, while :'VARIABLE'
is replaced by the variable's contents suitably quoted to become a single
shell-command argument. (The quoting rules for that are different from
those for SQL literals, so this is a bit of an abuse of the :'VARIABLE'
notation, but I doubt anyone will be confused.)
As with other situations in psql, no substitution occurs if the word
following a colon is not a known variable name. That limits the risk of
compatibility problems for existing psql scripts; but the risk isn't zero,
so this needs to be called out in the v10 release notes.
Discussion: https://postgr.es/m/9561.1490895211@sss.pgh.pa.us
2017-04-01 21:44:54 -04:00
|
|
|
extern bool appendShellStringNoError(PQExpBuffer buf, const char *str);
|
2016-08-08 10:07:46 -04:00
|
|
|
extern void appendConnStrVal(PQExpBuffer buf, const char *str);
|
2016-08-08 10:07:46 -04:00
|
|
|
extern void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname);
|
2016-08-08 10:07:46 -04:00
|
|
|
|
2016-03-24 15:55:44 -04:00
|
|
|
extern bool parsePGArray(const char *atext, char ***itemarray, int *nitems);
|
2021-12-06 12:39:45 -05:00
|
|
|
extern void appendPGArray(PQExpBuffer buffer, const char *value);
|
2016-03-24 15:55:44 -04:00
|
|
|
|
2016-05-06 07:45:36 -04:00
|
|
|
extern bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions,
|
|
|
|
|
const char *prefix, int encoding, bool std_strings);
|
|
|
|
|
|
2016-03-24 15:55:44 -04:00
|
|
|
extern bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf,
|
|
|
|
|
const char *pattern,
|
|
|
|
|
bool have_where, bool force_escape,
|
|
|
|
|
const char *schemavar, const char *namevar,
|
Allow db.schema.table patterns, but complain about random garbage.
psql, pg_dump, and pg_amcheck share code to process object name
patterns like 'foo*.bar*' to match all tables with names starting in
'bar' that are in schemas starting with 'foo'. Before v14, any number
of extra name parts were silently ignored, so a command line '\d
foo.bar.baz.bletch.quux' was interpreted as '\d bletch.quux'. In v14,
as a result of commit 2c8726c4b0a496608919d1f78a5abc8c9b6e0868, we
instead treated this as a request for table quux in a schema named
'foo.bar.baz.bletch'. That caused problems for people like Justin
Pryzby who were accustomed to copying strings of the form
db.schema.table from messages generated by PostgreSQL itself and using
them as arguments to \d.
Accordingly, revise things so that if an object name pattern contains
more parts than we're expecting, we throw an error, unless there's
exactly one extra part and it matches the current database name.
That way, thisdb.myschema.mytable is accepted as meaning just
myschema.mytable, but otherdb.myschema.mytable is an error, and so
is some.random.garbage.myschema.mytable.
Mark Dilger, per report from Justin Pryzby and discussion among
various people.
Discussion: https://www.postgresql.org/message-id/20211013165426.GD27491%40telsasoft.com
2022-04-20 11:02:35 -04:00
|
|
|
const char *altnamevar, const char *visibilityrule,
|
|
|
|
|
PQExpBuffer dbnamebuf, int *dotcnt);
|
2016-03-24 15:55:44 -04:00
|
|
|
|
Factor pattern-construction logic out of processSQLNamePattern.
The logic for converting the shell-glob-like syntax supported by
utilities like psql and pg_dump to regular expression is
extracted into a new function patternToSQLRegex. The existing
function processSQLNamePattern now uses this function as a
subroutine.
patternToSQLRegex is a little more general than what is required
by processSQLNamePattern. That function is only interested in
patterns that can have up to 2 parts, a schema and a relation;
but patternToSQLRegex can limit the maximum number of parts to
between 1 and 3, so that patterns can look like either
"database.schema.relation", "schema.relation", or "relation"
depending on how it's invoked and what the user specifies.
processSQLNamePattern only passes two buffers, so works exactly
the same as before, always interpreting the pattern as either
a "schema.relation" pattern or a "relation" pattern. But,
future callers can use this function in other ways.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether
any of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
2021-02-03 13:19:41 -05:00
|
|
|
extern void patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf,
|
|
|
|
|
PQExpBuffer schemabuf, PQExpBuffer namebuf,
|
Allow db.schema.table patterns, but complain about random garbage.
psql, pg_dump, and pg_amcheck share code to process object name
patterns like 'foo*.bar*' to match all tables with names starting in
'bar' that are in schemas starting with 'foo'. Before v14, any number
of extra name parts were silently ignored, so a command line '\d
foo.bar.baz.bletch.quux' was interpreted as '\d bletch.quux'. In v14,
as a result of commit 2c8726c4b0a496608919d1f78a5abc8c9b6e0868, we
instead treated this as a request for table quux in a schema named
'foo.bar.baz.bletch'. That caused problems for people like Justin
Pryzby who were accustomed to copying strings of the form
db.schema.table from messages generated by PostgreSQL itself and using
them as arguments to \d.
Accordingly, revise things so that if an object name pattern contains
more parts than we're expecting, we throw an error, unless there's
exactly one extra part and it matches the current database name.
That way, thisdb.myschema.mytable is accepted as meaning just
myschema.mytable, but otherdb.myschema.mytable is an error, and so
is some.random.garbage.myschema.mytable.
Mark Dilger, per report from Justin Pryzby and discussion among
various people.
Discussion: https://www.postgresql.org/message-id/20211013165426.GD27491%40telsasoft.com
2022-04-20 11:02:35 -04:00
|
|
|
const char *pattern, bool force_escape,
|
|
|
|
|
bool want_literal_dbname, int *dotcnt);
|
Factor pattern-construction logic out of processSQLNamePattern.
The logic for converting the shell-glob-like syntax supported by
utilities like psql and pg_dump to regular expression is
extracted into a new function patternToSQLRegex. The existing
function processSQLNamePattern now uses this function as a
subroutine.
patternToSQLRegex is a little more general than what is required
by processSQLNamePattern. That function is only interested in
patterns that can have up to 2 parts, a schema and a relation;
but patternToSQLRegex can limit the maximum number of parts to
between 1 and 3, so that patterns can look like either
"database.schema.relation", "schema.relation", or "relation"
depending on how it's invoked and what the user specifies.
processSQLNamePattern only passes two buffers, so works exactly
the same as before, always interpreting the pattern as either
a "schema.relation" pattern or a "relation" pattern. But,
future callers can use this function in other ways.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether
any of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
2021-02-03 13:19:41 -05:00
|
|
|
|
2016-03-24 15:55:44 -04:00
|
|
|
#endif /* STRING_UTILS_H */
|