Commit 4a66300a authored by Robert Haas's avatar Robert Haas

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 2c8726c4, 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
parent 7891a0d5
......@@ -3595,14 +3595,27 @@ select 1\; select 2\; select 3;
</para>
<para>
A pattern that contains a dot (<literal>.</literal>) is interpreted as a schema
A relation pattern that contains a dot (<literal>.</literal>) is interpreted as a schema
name pattern followed by an object name pattern. For example,
<literal>\dt foo*.*bar*</literal> displays all tables whose table name
includes <literal>bar</literal> that are in schemas whose schema name
starts with <literal>foo</literal>. When no dot appears, then the pattern
matches only objects that are visible in the current schema search path.
Again, a dot within double quotes loses its special meaning and is matched
literally.
literally. A relation pattern that contains two dots (<literal>.</literal>)
is interpreted as a database name followed by a schema name pattern followed
by an object name pattern. The database name portion will not be treated as
a pattern and must match the name of the currently connected database, else
an error will be raised.
</para>
<para>
A schema pattern that contains a dot (<literal>.</literal>) is interpreted
as a database name followed by a schema name pattern. For example,
<literal>\dn mydb.*foo*</literal> displays all schemas whose schema name
includes <literal>foo</literal>. The database name portion will not be
treated as a pattern and must match the name of the currently connected
database, else an error will be raised.
</para>
<para>
......
......@@ -1341,10 +1341,17 @@ static void
append_database_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
{
PQExpBufferData buf;
int dotcnt;
PatternInfo *info = extend_pattern_info_array(pia);
initPQExpBuffer(&buf);
patternToSQLRegex(encoding, NULL, NULL, &buf, pattern, false);
patternToSQLRegex(encoding, NULL, NULL, &buf, pattern, false, false,
&dotcnt);
if (dotcnt > 0)
{
pg_log_error("improper qualified name (too many dotted names): %s", pattern);
exit(2);
}
info->pattern = pattern;
info->db_regex = pstrdup(buf.data);
......@@ -1365,12 +1372,19 @@ append_schema_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
{
PQExpBufferData dbbuf;
PQExpBufferData nspbuf;
int dotcnt;
PatternInfo *info = extend_pattern_info_array(pia);
initPQExpBuffer(&dbbuf);
initPQExpBuffer(&nspbuf);
patternToSQLRegex(encoding, NULL, &dbbuf, &nspbuf, pattern, false);
patternToSQLRegex(encoding, NULL, &dbbuf, &nspbuf, pattern, false, false,
&dotcnt);
if (dotcnt > 1)
{
pg_log_error("improper qualified name (too many dotted names): %s", pattern);
exit(2);
}
info->pattern = pattern;
if (dbbuf.data[0])
{
......@@ -1402,13 +1416,20 @@ append_relation_pattern_helper(PatternInfoArray *pia, const char *pattern,
PQExpBufferData dbbuf;
PQExpBufferData nspbuf;
PQExpBufferData relbuf;
int dotcnt;
PatternInfo *info = extend_pattern_info_array(pia);
initPQExpBuffer(&dbbuf);
initPQExpBuffer(&nspbuf);
initPQExpBuffer(&relbuf);
patternToSQLRegex(encoding, &dbbuf, &nspbuf, &relbuf, pattern, false);
patternToSQLRegex(encoding, &dbbuf, &nspbuf, &relbuf, pattern, false,
false, &dotcnt);
if (dotcnt > 2)
{
pg_log_error("improper relation name (too many dotted names): %s", pattern);
exit(2);
}
info->pattern = pattern;
if (dbbuf.data[0])
{
......
......@@ -6,7 +6,7 @@ use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 76;
use Test::More tests => 100;
# Test set-up
my ($node, $port);
......@@ -147,6 +147,100 @@ $node->command_checks_all(
[qr/pg_amcheck: error: no heap tables to check matching "\."/],
'checking table pattern "."');
# Check that a multipart database name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'localhost.postgres' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): localhost\.postgres/
],
'multipart database patterns are rejected'
);
# Check that a three-part schema name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-s', 'localhost.postgres.pg_catalog' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): localhost\.postgres\.pg_catalog/
],
'three part schema patterns are rejected'
);
# Check that a four-part table name is rejected
$node->command_checks_all(
[ 'pg_amcheck', '-t', 'localhost.postgres.pg_catalog.pg_class' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): localhost\.postgres\.pg_catalog\.pg_class/
],
'four part table patterns are rejected'
);
# Check that too many dotted names still draws an error under --no-strict-names
# That flag means that it is ok for the object to be missing, not that it is ok
# for the object name to be ungrammatical
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-t', 'this.is.a.really.long.dotted.string' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): this\.is\.a\.really\.long\.dotted\.string/
],
'ungrammatical table names still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-s', 'postgres.long.dotted.string' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): postgres\.long\.dotted\.string/
],
'ungrammatical schema names still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-d', 'postgres.long.dotted.string' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): postgres\.long\.dotted\.string/
],
'ungrammatical database names still draw errors under --no-strict-names'
);
# Likewise for exclusion patterns
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-T', 'a.b.c.d' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper relation name \(too many dotted names\): a\.b\.c\.d/
],
'ungrammatical table exclusions still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-S', 'a.b.c' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): a\.b\.c/
],
'ungrammatical schema exclusions still draw errors under --no-strict-names'
);
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-D', 'a.b' ],
2,
[qr/^$/],
[
qr/pg_amcheck: error: improper qualified name \(too many dotted names\): a\.b/
],
'ungrammatical database exclusions still draw errors under --no-strict-names'
);
#########################################
# Test checking non-existent databases, schemas, tables, and indexes
......@@ -165,9 +259,7 @@ $node->command_checks_all(
'-d', 'no*such*database',
'-r', 'none.none',
'-r', 'none.none.none',
'-r', 'this.is.a.really.long.dotted.string',
'-r', 'postgres.none.none',
'-r', 'postgres.long.dotted.string',
'-r', 'postgres.pg_catalog.none',
'-r', 'postgres.none.pg_class',
'-t', 'postgres.pg_catalog.pg_class', # This exists
......@@ -186,15 +278,12 @@ $node->command_checks_all(
qr/pg_amcheck: warning: no connectable databases to check matching "no\*such\*database"/,
qr/pg_amcheck: warning: no relations to check matching "none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "none\.none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "this\.is\.a\.really\.long\.dotted\.string"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.long\.dotted\.string"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.pg_catalog\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.pg_class"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no\*such\*database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "none\.none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "this\.is\.a\.really\.long\.dotted\.string"/,
],
'many unmatched patterns and one matched pattern under --no-strict-names'
);
......
......@@ -164,6 +164,9 @@ static void expand_table_name_patterns(Archive *fout,
SimpleStringList *patterns,
SimpleOidList *oids,
bool strict_names);
static void prohibit_crossdb_refs(PGconn *conn, const char *dbname,
const char *pattern);
static NamespaceInfo *findNamespace(Oid nsoid);
static void dumpTableData(Archive *fout, const TableDataInfo *tdinfo);
static void refreshMatViewData(Archive *fout, const TableDataInfo *tdinfo);
......@@ -1358,10 +1361,21 @@ expand_schema_name_patterns(Archive *fout,
for (cell = patterns->head; cell; cell = cell->next)
{
PQExpBufferData dbbuf;
int dotcnt;
appendPQExpBufferStr(query,
"SELECT oid FROM pg_catalog.pg_namespace n\n");
initPQExpBuffer(&dbbuf);
processSQLNamePattern(GetConnection(fout), query, cell->val, false,
false, NULL, "n.nspname", NULL, NULL);
false, NULL, "n.nspname", NULL, NULL, &dbbuf,
&dotcnt);
if (dotcnt > 1)
fatal("improper qualified name (too many dotted names): %s",
cell->val);
else if (dotcnt == 1)
prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
termPQExpBuffer(&dbbuf);
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (strict_names && PQntuples(res) == 0)
......@@ -1405,10 +1419,16 @@ expand_extension_name_patterns(Archive *fout,
*/
for (cell = patterns->head; cell; cell = cell->next)
{
int dotcnt;
appendPQExpBufferStr(query,
"SELECT oid FROM pg_catalog.pg_extension e\n");
processSQLNamePattern(GetConnection(fout), query, cell->val, false,
false, NULL, "e.extname", NULL, NULL);
false, NULL, "e.extname", NULL, NULL, NULL,
&dotcnt);
if (dotcnt > 0)
fatal("improper qualified name (too many dotted names): %s",
cell->val);
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (strict_names && PQntuples(res) == 0)
......@@ -1452,10 +1472,16 @@ expand_foreign_server_name_patterns(Archive *fout,
for (cell = patterns->head; cell; cell = cell->next)
{
int dotcnt;
appendPQExpBufferStr(query,
"SELECT oid FROM pg_catalog.pg_foreign_server s\n");
processSQLNamePattern(GetConnection(fout), query, cell->val, false,
false, NULL, "s.srvname", NULL, NULL);
false, NULL, "s.srvname", NULL, NULL, NULL,
&dotcnt);
if (dotcnt > 0)
fatal("improper qualified name (too many dotted names): %s",
cell->val);
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
if (PQntuples(res) == 0)
......@@ -1498,6 +1524,9 @@ expand_table_name_patterns(Archive *fout,
for (cell = patterns->head; cell; cell = cell->next)
{
PQExpBufferData dbbuf;
int dotcnt;
/*
* Query must remain ABSOLUTELY devoid of unqualified names. This
* would be unnecessary given a pg_table_is_visible() variant taking a
......@@ -1513,9 +1542,17 @@ expand_table_name_patterns(Archive *fout,
RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
RELKIND_PARTITIONED_TABLE);
initPQExpBuffer(&dbbuf);
processSQLNamePattern(GetConnection(fout), query, cell->val, true,
false, "n.nspname", "c.relname", NULL,
"pg_catalog.pg_table_is_visible(c.oid)");
"pg_catalog.pg_table_is_visible(c.oid)", &dbbuf,
&dotcnt);
if (dotcnt > 2)
fatal("improper relation name (too many dotted names): %s",
cell->val);
else if (dotcnt == 2)
prohibit_crossdb_refs(GetConnection(fout), dbbuf.data, cell->val);
termPQExpBuffer(&dbbuf);
ExecuteSqlStatement(fout, "RESET search_path");
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
......@@ -1536,6 +1573,25 @@ expand_table_name_patterns(Archive *fout,
destroyPQExpBuffer(query);
}
/*
* Verifies that the connected database name matches the given database name,
* and if not, dies with an error about the given pattern.
*
* The 'dbname' argument should be a literal name parsed from 'pattern'.
*/
static void
prohibit_crossdb_refs(PGconn *conn, const char *dbname, const char *pattern)
{
const char *db;
db = PQdb(conn);
if (db == NULL)
fatal("You are currently not connected to a database.");
if (strcmp(db, dbname) != 0)
fatal("cross-database references are not implemented: %s", pattern);
}
/*
* checkExtensionMembership
* Determine whether object is an extension member, and if so,
......
......@@ -1438,10 +1438,21 @@ expand_dbname_patterns(PGconn *conn,
for (SimpleStringListCell *cell = patterns->head; cell; cell = cell->next)
{
int dotcnt;
appendPQExpBufferStr(query,
"SELECT datname FROM pg_catalog.pg_database n\n");
processSQLNamePattern(conn, query, cell->val, false,
false, NULL, "datname", NULL, NULL);
false, NULL, "datname", NULL, NULL, NULL,
&dotcnt);
if (dotcnt > 0)
{
pg_log_error("improper qualified name (too many dotted names): %s",
cell->val);
PQfinish(conn);
exit_nicely(1);
}
res = executeQuery(conn, query->data);
for (int i = 0; i < PQntuples(res); i++)
......
......@@ -3602,7 +3602,7 @@ $node->psql('postgres', 'create database regress_pg_dump_test;');
# Start with number of command_fails_like()*2 tests below (each
# command_fails_like is actually 2 tests)
my $num_tests = 12;
my $num_tests = 42;
foreach my $run (sort keys %pgdump_runs)
{
......@@ -3775,6 +3775,113 @@ command_fails_like(
qr/\Qpg_dump: error: no matching tables were found for pattern\E/,
'no matching tables');
#########################################
# Test invalid multipart database names
command_fails_like(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', '.' ],
qr/pg_dumpall: error: improper qualified name \(too many dotted names\): \./,
'pg_dumpall: option --exclude-database rejects multipart pattern "."'
);
command_fails_like(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', '.*' ],
qr/pg_dumpall: error: improper qualified name \(too many dotted names\): \.\*/,
'pg_dumpall: option --exclude-database rejects multipart pattern ".*"'
);
command_fails_like(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', '*.*' ],
qr/pg_dumpall: error: improper qualified name \(too many dotted names\): \*\.\*/,
'pg_dumpall: option --exclude-database rejects multipart pattern "*.*"'
);
command_fails_like(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', 'myhost.mydb' ],
qr/pg_dumpall: error: improper qualified name \(too many dotted names\): myhost\.mydb/,
'pg_dumpall: option --exclude-database rejects multipart database names'
);
#########################################
# Test valid database exclusion patterns
$node->command_ok(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', '"myhost.mydb"' ],
'pg_dumpall: option --exclude-database handles database names with embedded dots'
);
$node->command_ok(
[ 'pg_dumpall', '-p', "$port", '--exclude-database', '??*' ],
'pg_dumpall: option --exclude-database handles database name patterns'
);
#########################################
# Test invalid multipart schema names
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', 'myhost.mydb.myschema' ],
qr/pg_dump: error: improper qualified name \(too many dotted names\): myhost\.mydb\.myschema/,
'pg_dump: option --schema rejects three-part schema names'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', 'otherdb.myschema' ],
qr/pg_dump: error: cross-database references are not implemented: otherdb\.myschema/,
'pg_dump: option --schema rejects cross-database multipart schema names'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', '"some.other.db".myschema' ],
qr/pg_dump: error: cross-database references are not implemented: "some\.other\.db"\.myschema/,
'pg_dump: option --schema rejects cross-database multipart schema names with embedded dots'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', '.' ],
qr/pg_dump: error: cross-database references are not implemented: \./,
'pg_dump: option --schema rejects degenerate two-part schema name: "."'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', '.*' ],
qr/pg_dump: error: cross-database references are not implemented: \.\*/,
'pg_dump: option --schema rejects degenerate two-part schema name: ".*"'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', '..' ],
qr/pg_dump: error: improper qualified name \(too many dotted names\): \.\./,
'pg_dump: option --schema rejects degenerate three-part schema name: ".."'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--schema', '.*.*' ],
qr/pg_dump: error: improper qualified name \(too many dotted names\): \.\*\.\*/,
'pg_dump: option --schema rejects degenerate three-part schema pattern: ".*.*"'
);
#########################################
# Test invalid multipart relation names
command_fails_like(
[ 'pg_dump', '-p', "$port", '--table', 'myhost.mydb.myschema.mytable' ],
qr/pg_dump: error: improper relation name \(too many dotted names\): myhost\.mydb\.myschema\.mytable/,
'pg_dump: option --table rejects four-part table names'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--table', 'otherdb.pg_catalog.pg_class' ],
qr/pg_dump: error: cross-database references are not implemented: otherdb\.pg_catalog\.pg_class/,
'pg_dump: option --table rejects cross-database three part table names'
);
command_fails_like(
[ 'pg_dump', '-p', "$port", '--table', '"some.other.db".pg_catalog.pg_class' ],
qr/pg_dump: error: cross-database references are not implemented: "some\.other\.db"\.pg_catalog\.pg_class/,
'pg_dump: option --table rejects cross-database three part table names with embedded dots'
);
#########################################
# Run all runs
......
This diff is collapsed.
......@@ -819,6 +819,9 @@ appendReloptionsArray(PQExpBuffer buffer, const char *reloptions,
* altnamevar: NULL, or name of an alternative variable to match against name.
* visibilityrule: clause to use if we want to restrict to visible objects
* (for example, "pg_catalog.pg_table_is_visible(p.oid)"). Can be NULL.
* dbnamebuf: output parameter receiving the database name portion of the
* pattern, if any. Can be NULL.
* dotcnt: how many separators were parsed from the pattern, by reference.
*
* Formatting note: the text already present in buf should end with a newline.
* The appended text, if any, will end with one too.
......@@ -827,16 +830,21 @@ 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)
const char *altnamevar, const char *visibilityrule,
PQExpBuffer dbnamebuf, int *dotcnt)
{
PQExpBufferData schemabuf;
PQExpBufferData namebuf;
bool added_clause = false;
int dcnt;
#define WHEREAND() \
(appendPQExpBufferStr(buf, have_where ? " AND " : "WHERE "), \
have_where = true, added_clause = true)
if (dotcnt == NULL)
dotcnt = &dcnt;
*dotcnt = 0;
if (pattern == NULL)
{
/* Default: select all visible objects */
......@@ -856,8 +864,10 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
* execute. Quoting/escaping into SQL literal format will be done below
* using appendStringLiteralConn().
*/
patternToSQLRegex(PQclientEncoding(conn), NULL, &schemabuf, &namebuf,
pattern, force_escape);
patternToSQLRegex(PQclientEncoding(conn),
(schemavar ? dbnamebuf : NULL),
(schemavar ? &schemabuf: NULL),
&namebuf, pattern, force_escape, true, dotcnt);
/*
* Now decide what we need to emit. We may run under a hostile
......@@ -870,7 +880,7 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
* is >= v12 then we need to force it through explicit COLLATE clauses,
* otherwise the "C" collation attached to "name" catalog columns wins.
*/
if (namebuf.len > 2)
if (namevar && namebuf.len > 2)
{
/* We have a name pattern, so constrain the namevar(s) */
......@@ -904,7 +914,7 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
}
}
if (schemabuf.len > 2)
if (schemavar && schemabuf.len > 2)
{
/* We have a schema pattern, so constrain the schemavar */
......@@ -945,8 +955,7 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
* If the dbnamebuf and schemabuf arguments are non-NULL, and the pattern
* contains two or more dbname/schema/name separators, we parse the portions of
* the pattern prior to the first and second separators into dbnamebuf and
* schemabuf, and the rest into namebuf. (Additional dots in the name portion
* are not treated as special.)
* schemabuf, and the rest into namebuf.
*
* If dbnamebuf is NULL and schemabuf is non-NULL, and the pattern contains at
* least one separator, we parse the first portion into schemabuf and the rest
......@@ -954,24 +963,49 @@ processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern,
*
* Otherwise, we parse all the pattern into namebuf.
*
* If the pattern contains more dotted parts than buffers to parse into, the
* extra dots will be treated as literal characters and written into the
* namebuf, though they will be counted. Callers should always check the value
* returned by reference in dotcnt and handle this error case appropriately.
*
* We surround the regexps with "^(...)$" to force them to match whole strings,
* as per SQL practice. We have to have parens in case strings contain "|",
* else the "^" and "$" will be bound into the first and last alternatives
* which is not what we want.
* which is not what we want. Whether this is done for dbnamebuf is controlled
* by the want_literal_dbname parameter.
*
* The regexps we parse into the buffers are appended to the data (if any)
* already present. If we parse fewer fields than the number of buffers we
* were given, the extra buffers are unaltered.
*
* encoding: the character encoding for the given pattern
* dbnamebuf: output parameter receiving the database name portion of the
* pattern, if any. Can be NULL.
* schemabuf: output parameter receiving the schema name portion of the
* pattern, if any. Can be NULL.
* namebuf: output parameter receiving the database name portion of the
* pattern, if any. Can be NULL.
* pattern: user-specified pattern option, or NULL if none ("*" is implied).
* force_escape: always quote regexp special characters, even outside
* double quotes (else they are quoted only between double quotes).
* want_literal_dbname: if true, regexp special characters within the database
* name portion of the pattern will not be escaped, nor will the dbname be
* converted into a regular expression.
* dotcnt: output parameter receiving the number of separators parsed from the
* pattern.
*/
void
patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
PQExpBuffer namebuf, const char *pattern, bool force_escape)
PQExpBuffer namebuf, const char *pattern, bool force_escape,
bool want_literal_dbname, int *dotcnt)
{
PQExpBufferData buf[3];
PQExpBufferData left_literal;
PQExpBuffer curbuf;
PQExpBuffer maxbuf;
int i;
bool inquotes;
bool left;
const char *cp;
Assert(pattern != NULL);
......@@ -979,7 +1013,9 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
/* callers should never expect "dbname.relname" format */
Assert(dbnamebuf == NULL || schemabuf != NULL);
Assert(dotcnt != NULL);
*dotcnt = 0;
inquotes = false;
cp = pattern;
......@@ -991,6 +1027,13 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
maxbuf = &buf[0];
curbuf = &buf[0];
if (want_literal_dbname)
{
left = true;
initPQExpBuffer(&left_literal);
}
else
left = false;
initPQExpBuffer(curbuf);
appendPQExpBufferStr(curbuf, "^(");
while (*cp)
......@@ -1003,6 +1046,8 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
{
/* emit one quote, stay in inquotes mode */
appendPQExpBufferChar(curbuf, '"');
if (left)
appendPQExpBufferChar(&left_literal, '"');
cp++;
}
else
......@@ -1013,32 +1058,40 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
{
appendPQExpBufferChar(curbuf,
pg_tolower((unsigned char) ch));
if (left)
appendPQExpBufferChar(&left_literal,
pg_tolower((unsigned char) ch));
cp++;
}
else if (!inquotes && ch == '*')
{
appendPQExpBufferStr(curbuf, ".*");
if (left)
appendPQExpBufferChar(&left_literal, '*');
cp++;
}
else if (!inquotes && ch == '?')
{
appendPQExpBufferChar(curbuf, '.');
if (left)
appendPQExpBufferChar(&left_literal, '?');
cp++;
}
/*
* When we find a dbname/schema/name separator, we treat it specially
* only if the caller requested more patterns to be parsed than we
* have already parsed from the pattern. Otherwise, dot characters
* are not special.
*/
else if (!inquotes && ch == '.' && curbuf < maxbuf)
else if (!inquotes && ch == '.')
{
appendPQExpBufferStr(curbuf, ")$");
curbuf++;
initPQExpBuffer(curbuf);
appendPQExpBufferStr(curbuf, "^(");
cp++;
left = false;
if (dotcnt)
(*dotcnt)++;
if (curbuf < maxbuf)
{
appendPQExpBufferStr(curbuf, ")$");
curbuf++;
initPQExpBuffer(curbuf);
appendPQExpBufferStr(curbuf, "^(");
cp++;
}
else
appendPQExpBufferChar(curbuf, *cp++);
}
else if (ch == '$')
{
......@@ -1050,6 +1103,8 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
* having it possess its regexp meaning.
*/
appendPQExpBufferStr(curbuf, "\\$");
if (left)
appendPQExpBufferChar(&left_literal, '$');
cp++;
}
else
......@@ -1074,25 +1129,35 @@ patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf, PQExpBuffer schemabuf,
appendPQExpBufferChar(curbuf, '\\');
i = PQmblenBounded(cp, encoding);
while (i--)
{
if (left)
appendPQExpBufferChar(&left_literal, *cp);
appendPQExpBufferChar(curbuf, *cp++);
}
}
}
appendPQExpBufferStr(curbuf, ")$");
appendPQExpBufferStr(namebuf, curbuf->data);
termPQExpBuffer(curbuf);
if (curbuf > buf)
if (namebuf)
{
appendPQExpBufferStr(namebuf, curbuf->data);
termPQExpBuffer(curbuf);
curbuf--;
}
if (schemabuf && curbuf >= buf)
{
appendPQExpBufferStr(schemabuf, curbuf->data);
termPQExpBuffer(curbuf);
curbuf--;
}
if (curbuf > buf)
{
curbuf--;
if (dbnamebuf && curbuf >= buf)
{
if (want_literal_dbname)
appendPQExpBufferStr(dbnamebuf, left_literal.data);
else
appendPQExpBufferStr(dbnamebuf, curbuf->data);
termPQExpBuffer(curbuf);
}
termPQExpBuffer(curbuf);
}
}
......@@ -54,10 +54,12 @@ extern 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);
const char *altnamevar, const char *visibilityrule,
PQExpBuffer dbnamebuf, int *dotcnt);
extern void patternToSQLRegex(int encoding, PQExpBuffer dbnamebuf,
PQExpBuffer schemabuf, PQExpBuffer namebuf,
const char *pattern, bool force_escape);
const char *pattern, bool force_escape,
bool want_literal_dbname, int *dotcnt);
#endif /* STRING_UTILS_H */
This diff is collapsed.
......@@ -1241,3 +1241,245 @@ drop role regress_partitioning_role;
\dfa bit* small*
\do - pg_catalog.int4
\do && anyarray *
-- check describing invalid multipart names
\dA regression.heap
\dA nonesuch.heap
\dt host.regression.pg_catalog.pg_class
\dt |.pg_catalog.pg_class
\dt nonesuch.pg_catalog.pg_class
\da host.regression.pg_catalog.sum
\da +.pg_catalog.sum
\da nonesuch.pg_catalog.sum
\dAc nonesuch.brin
\dAc regression.brin
\dAf nonesuch.brin
\dAf regression.brin
\dAo nonesuch.brin
\dAo regression.brin
\dAp nonesuch.brin
\dAp regression.brin
\db nonesuch.pg_default
\db regression.pg_default
\dc host.regression.public.conversion
\dc (.public.conversion
\dc nonesuch.public.conversion
\dC host.regression.pg_catalog.int8
\dC ).pg_catalog.int8
\dC nonesuch.pg_catalog.int8
\dd host.regression.pg_catalog.pg_class
\dd [.pg_catalog.pg_class
\dd nonesuch.pg_catalog.pg_class
\dD host.regression.public.gtestdomain1
\dD ].public.gtestdomain1
\dD nonesuch.public.gtestdomain1
\ddp host.regression.pg_catalog.pg_class
\ddp {.pg_catalog.pg_class
\ddp nonesuch.pg_catalog.pg_class
\dE host.regression.public.ft
\dE }.public.ft
\dE nonesuch.public.ft
\di host.regression.public.tenk1_hundred
\di ..public.tenk1_hundred
\di nonesuch.public.tenk1_hundred
\dm host.regression.public.mvtest_bb
\dm ^.public.mvtest_bb
\dm nonesuch.public.mvtest_bb
\ds host.regression.public.check_seq
\ds regression|mydb.public.check_seq
\ds nonesuch.public.check_seq
\dt host.regression.public.b_star
\dt regres+ion.public.b_star
\dt nonesuch.public.b_star
\dv host.regression.public.shoe
\dv regress(ion).public.shoe
\dv nonesuch.public.shoe
\des nonesuch.server
\des regression.server
\des nonesuch.server
\des regression.server
\des nonesuch.username
\des regression.username
\dew nonesuch.fdw
\dew regression.fdw
\df host.regression.public.namelen
\df regres[qrstuv]ion.public.namelen
\df nonesuch.public.namelen
\dF host.regression.pg_catalog.arabic
\dF regres{1,2}ion.pg_catalog.arabic
\dF nonesuch.pg_catalog.arabic
\dFd host.regression.pg_catalog.arabic_stem
\dFd regres?ion.pg_catalog.arabic_stem
\dFd nonesuch.pg_catalog.arabic_stem
\dFp host.regression.pg_catalog.default
\dFp ^regression.pg_catalog.default
\dFp nonesuch.pg_catalog.default
\dFt host.regression.pg_catalog.ispell
\dFt regression$.pg_catalog.ispell
\dFt nonesuch.pg_catalog.ispell
\dg nonesuch.pg_database_owner
\dg regression.pg_database_owner
\dL host.regression.plpgsql
\dL *.plpgsql
\dL nonesuch.plpgsql
\dn host.regression.public
\dn """".public
\dn nonesuch.public
\do host.regression.public.!=-
\do "regression|mydb".public.!=-
\do nonesuch.public.!=-
\dO host.regression.pg_catalog.POSIX
\dO .pg_catalog.POSIX
\dO nonesuch.pg_catalog.POSIX
\dp host.regression.public.a_star
\dp "regres+ion".public.a_star
\dp nonesuch.public.a_star
\dP host.regression.public.mlparted
\dP "regres(sion)".public.mlparted
\dP nonesuch.public.mlparted
\drds nonesuch.lc_messages
\drds regression.lc_messages
\dRp public.mypub
\dRp regression.mypub
\dRs public.mysub
\dRs regression.mysub
\dT host.regression.public.widget
\dT "regression{1,2}".public.widget
\dT nonesuch.public.widget
\dx regression.plpgsql
\dx nonesuch.plpgsql
\dX host.regression.public.func_deps_stat
\dX "^regression$".public.func_deps_stat
\dX nonesuch.public.func_deps_stat
\dy regression.myevt
\dy nonesuch.myevt
-- check that dots within quoted name segments are not counted
\dA "no.such.access.method"
\dt "no.such.table.relation"
\da "no.such.aggregate.function"
\dAc "no.such.operator.class"
\dAf "no.such.operator.family"
\dAo "no.such.operator.of.operator.family"
\dAp "no.such.operator.support.function.of.operator.family"
\db "no.such.tablespace"
\dc "no.such.conversion"
\dC "no.such.cast"
\dd "no.such.object.description"
\dD "no.such.domain"
\ddp "no.such.default.access.privilege"
\di "no.such.index.relation"
\dm "no.such.materialized.view"
\ds "no.such.relation"
\dt "no.such.relation"
\dv "no.such.relation"
\des "no.such.foreign.server"
\dew "no.such.foreign.data.wrapper"
\df "no.such.function"
\dF "no.such.text.search.configuration"
\dFd "no.such.text.search.dictionary"
\dFp "no.such.text.search.parser"
\dFt "no.such.text.search.template"
\dg "no.such.role"
\dL "no.such.language"
\dn "no.such.schema"
\do "no.such.operator"
\dO "no.such.collation"
\dp "no.such.access.privilege"
\dP "no.such.partitioned.relation"
\drds "no.such.setting"
\dRp "no.such.publication"
\dRs "no.such.subscription"
\dT "no.such.data.type"
\dx "no.such.installed.extension"
\dX "no.such.extended.statistics"
\dy "no.such.event.trigger"
-- again, but with dotted schema qualifications.
\dA "no.such.schema"."no.such.access.method"
\dt "no.such.schema"."no.such.table.relation"
\da "no.such.schema"."no.such.aggregate.function"
\dAc "no.such.schema"."no.such.operator.class"
\dAf "no.such.schema"."no.such.operator.family"
\dAo "no.such.schema"."no.such.operator.of.operator.family"
\dAp "no.such.schema"."no.such.operator.support.function.of.operator.family"
\db "no.such.schema"."no.such.tablespace"
\dc "no.such.schema"."no.such.conversion"
\dC "no.such.schema"."no.such.cast"
\dd "no.such.schema"."no.such.object.description"
\dD "no.such.schema"."no.such.domain"
\ddp "no.such.schema"."no.such.default.access.privilege"
\di "no.such.schema"."no.such.index.relation"
\dm "no.such.schema"."no.such.materialized.view"
\ds "no.such.schema"."no.such.relation"
\dt "no.such.schema"."no.such.relation"
\dv "no.such.schema"."no.such.relation"
\des "no.such.schema"."no.such.foreign.server"
\dew "no.such.schema"."no.such.foreign.data.wrapper"
\df "no.such.schema"."no.such.function"
\dF "no.such.schema"."no.such.text.search.configuration"
\dFd "no.such.schema"."no.such.text.search.dictionary"
\dFp "no.such.schema"."no.such.text.search.parser"
\dFt "no.such.schema"."no.such.text.search.template"
\dg "no.such.schema"."no.such.role"
\dL "no.such.schema"."no.such.language"
\do "no.such.schema"."no.such.operator"
\dO "no.such.schema"."no.such.collation"
\dp "no.such.schema"."no.such.access.privilege"
\dP "no.such.schema"."no.such.partitioned.relation"
\drds "no.such.schema"."no.such.setting"
\dRp "no.such.schema"."no.such.publication"
\dRs "no.such.schema"."no.such.subscription"
\dT "no.such.schema"."no.such.data.type"
\dx "no.such.schema"."no.such.installed.extension"
\dX "no.such.schema"."no.such.extended.statistics"
\dy "no.such.schema"."no.such.event.trigger"
-- again, but with current database and dotted schema qualifications.
\dt regression."no.such.schema"."no.such.table.relation"
\da regression."no.such.schema"."no.such.aggregate.function"
\dc regression."no.such.schema"."no.such.conversion"
\dC regression."no.such.schema"."no.such.cast"
\dd regression."no.such.schema"."no.such.object.description"
\dD regression."no.such.schema"."no.such.domain"
\di regression."no.such.schema"."no.such.index.relation"
\dm regression."no.such.schema"."no.such.materialized.view"
\ds regression."no.such.schema"."no.such.relation"
\dt regression."no.such.schema"."no.such.relation"
\dv regression."no.such.schema"."no.such.relation"
\df regression."no.such.schema"."no.such.function"
\dF regression."no.such.schema"."no.such.text.search.configuration"
\dFd regression."no.such.schema"."no.such.text.search.dictionary"
\dFp regression."no.such.schema"."no.such.text.search.parser"
\dFt regression."no.such.schema"."no.such.text.search.template"
\do regression."no.such.schema"."no.such.operator"
\dO regression."no.such.schema"."no.such.collation"
\dp regression."no.such.schema"."no.such.access.privilege"
\dP regression."no.such.schema"."no.such.partitioned.relation"
\dT regression."no.such.schema"."no.such.data.type"
\dX regression."no.such.schema"."no.such.extended.statistics"
-- again, but with dotted database and dotted schema qualifications.
\dt "no.such.database"."no.such.schema"."no.such.table.relation"
\da "no.such.database"."no.such.schema"."no.such.aggregate.function"
\dc "no.such.database"."no.such.schema"."no.such.conversion"
\dC "no.such.database"."no.such.schema"."no.such.cast"
\dd "no.such.database"."no.such.schema"."no.such.object.description"
\dD "no.such.database"."no.such.schema"."no.such.domain"
\ddp "no.such.database"."no.such.schema"."no.such.default.access.privilege"
\di "no.such.database"."no.such.schema"."no.such.index.relation"
\dm "no.such.database"."no.such.schema"."no.such.materialized.view"
\ds "no.such.database"."no.such.schema"."no.such.relation"
\dt "no.such.database"."no.such.schema"."no.such.relation"
\dv "no.such.database"."no.such.schema"."no.such.relation"
\df "no.such.database"."no.such.schema"."no.such.function"
\dF "no.such.database"."no.such.schema"."no.such.text.search.configuration"
\dFd "no.such.database"."no.such.schema"."no.such.text.search.dictionary"
\dFp "no.such.database"."no.such.schema"."no.such.text.search.parser"
\dFt "no.such.database"."no.such.schema"."no.such.text.search.template"
\do "no.such.database"."no.such.schema"."no.such.operator"
\dO "no.such.database"."no.such.schema"."no.such.collation"
\dp "no.such.database"."no.such.schema"."no.such.access.privilege"
\dP "no.such.database"."no.such.schema"."no.such.partitioned.relation"
\dT "no.such.database"."no.such.schema"."no.such.data.type"
\dX "no.such.database"."no.such.schema"."no.such.extended.statistics"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment