Commit e3b7a6d1 authored by Tom Lane's avatar Tom Lane

Rationalize Query_for_list_of_[relations] query names in tab-complete.c.

The previous convention was to use names based on the set of relkinds being
selected for, which was not at all helpful for maintenance, especially
since people had been quite inconsistent about whether to change the names
when they changed the relkinds being selected for.  Instead, use names
based on the functionality we need the relation to have, following the
model established by Query_for_list_of_updatables.

While at it, sort the list of Query constants a bit better; it had the
distinct air of code-assembled-by-dartboard before.

Discussion: https://postgr.es/m/14830.1537481254@sss.pgh.pa.us
parent f025bd2d
...@@ -446,16 +446,6 @@ static const SchemaQuery Query_for_list_of_functions[] = { ...@@ -446,16 +446,6 @@ static const SchemaQuery Query_for_list_of_functions[] = {
} }
}; };
static const SchemaQuery Query_for_list_of_indexes = {
.catname = "pg_catalog.pg_class c",
.selcondition =
"c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.relname)",
};
static const SchemaQuery Query_for_list_of_procedures[] = { static const SchemaQuery Query_for_list_of_procedures[] = {
{ {
.min_server_version = 110000, .min_server_version = 110000,
...@@ -512,27 +502,33 @@ static const SchemaQuery Query_for_list_of_partitioned_tables = { ...@@ -512,27 +502,33 @@ static const SchemaQuery Query_for_list_of_partitioned_tables = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_constraints_with_schema = { static const SchemaQuery Query_for_list_of_views = {
.catname = "pg_catalog.pg_constraint c", .catname = "pg_catalog.pg_class c",
.selcondition = "c.conrelid <> 0", .selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
.viscondition = "true", /* there is no pg_constraint_is_visible */ .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.connamespace", .namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.conname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
/* Relations supporting INSERT, UPDATE or DELETE */ static const SchemaQuery Query_for_list_of_matviews = {
static const SchemaQuery Query_for_list_of_updatables = { .catname = "pg_catalog.pg_class c",
.selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.relname)",
};
static const SchemaQuery Query_for_list_of_indexes = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.selcondition = .selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " "c.relkind IN (" CppAsString2(RELKIND_INDEX) ", "
CppAsString2(RELKIND_FOREIGN_TABLE) ", " CppAsString2(RELKIND_PARTITIONED_INDEX) ")",
CppAsString2(RELKIND_VIEW) ", "
CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)", .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.relnamespace", .namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
/* All relations */
static const SchemaQuery Query_for_list_of_relations = { static const SchemaQuery Query_for_list_of_relations = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)", .viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
...@@ -540,7 +536,21 @@ static const SchemaQuery Query_for_list_of_relations = { ...@@ -540,7 +536,21 @@ static const SchemaQuery Query_for_list_of_relations = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_tsvmf = { /* Relations supporting INSERT, UPDATE or DELETE */
static const SchemaQuery Query_for_list_of_updatables = {
.catname = "pg_catalog.pg_class c",
.selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
CppAsString2(RELKIND_FOREIGN_TABLE) ", "
CppAsString2(RELKIND_VIEW) ", "
CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.relname)",
};
/* Relations supporting SELECT */
static const SchemaQuery Query_for_list_of_selectables = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.selcondition = .selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
...@@ -554,7 +564,11 @@ static const SchemaQuery Query_for_list_of_tsvmf = { ...@@ -554,7 +564,11 @@ static const SchemaQuery Query_for_list_of_tsvmf = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_tmf = { /* Relations supporting GRANT are currently same as those supporting SELECT */
#define Query_for_list_of_grantables Query_for_list_of_selectables
/* Relations supporting ANALYZE */
static const SchemaQuery Query_for_list_of_analyzables = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.selcondition = .selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
...@@ -566,7 +580,8 @@ static const SchemaQuery Query_for_list_of_tmf = { ...@@ -566,7 +580,8 @@ static const SchemaQuery Query_for_list_of_tmf = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_tpm = { /* Relations supporting index creation */
static const SchemaQuery Query_for_list_of_indexables = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.selcondition = .selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
...@@ -577,7 +592,8 @@ static const SchemaQuery Query_for_list_of_tpm = { ...@@ -577,7 +592,8 @@ static const SchemaQuery Query_for_list_of_tpm = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_tm = { /* Relations supporting VACUUM */
static const SchemaQuery Query_for_list_of_vacuumables = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_class c",
.selcondition = .selcondition =
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
...@@ -587,20 +603,15 @@ static const SchemaQuery Query_for_list_of_tm = { ...@@ -587,20 +603,15 @@ static const SchemaQuery Query_for_list_of_tm = {
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.relname)",
}; };
static const SchemaQuery Query_for_list_of_views = { /* Relations supporting CLUSTER are currently same as those supporting VACUUM */
.catname = "pg_catalog.pg_class c", #define Query_for_list_of_clusterables Query_for_list_of_vacuumables
.selcondition = "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)",
.namespace = "c.relnamespace",
.result = "pg_catalog.quote_ident(c.relname)",
};
static const SchemaQuery Query_for_list_of_matviews = { static const SchemaQuery Query_for_list_of_constraints_with_schema = {
.catname = "pg_catalog.pg_class c", .catname = "pg_catalog.pg_constraint c",
.selcondition = "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", .selcondition = "c.conrelid <> 0",
.viscondition = "pg_catalog.pg_table_is_visible(c.oid)", .viscondition = "true", /* there is no pg_constraint_is_visible */
.namespace = "c.relnamespace", .namespace = "c.connamespace",
.result = "pg_catalog.quote_ident(c.relname)", .result = "pg_catalog.quote_ident(c.conname)",
}; };
static const SchemaQuery Query_for_list_of_statistics = { static const SchemaQuery Query_for_list_of_statistics = {
...@@ -2169,9 +2180,9 @@ psql_completion(const char *text, int start, int end) ...@@ -2169,9 +2180,9 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH_CONST("("); COMPLETE_WITH_CONST("(");
/* CLUSTER */ /* CLUSTER */
else if (Matches1("CLUSTER")) else if (Matches1("CLUSTER"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, "UNION SELECT 'VERBOSE'"); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables, "UNION SELECT 'VERBOSE'");
else if (Matches2("CLUSTER", "VERBOSE")) else if (Matches2("CLUSTER", "VERBOSE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_clusterables, NULL);
/* If we have CLUSTER <sth>, then add "USING" */ /* If we have CLUSTER <sth>, then add "USING" */
else if (Matches2("CLUSTER", MatchAnyExcept("VERBOSE|ON"))) else if (Matches2("CLUSTER", MatchAnyExcept("VERBOSE|ON")))
COMPLETE_WITH_CONST("USING"); COMPLETE_WITH_CONST("USING");
...@@ -2326,11 +2337,11 @@ psql_completion(const char *text, int start, int end) ...@@ -2326,11 +2337,11 @@ psql_completion(const char *text, int start, int end)
/* /*
* Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations * Complete ... INDEX|CONCURRENTLY [<name>] ON with a list of relations
* that can indexes can be created on * that indexes can be created on
*/ */
else if (TailMatches3("INDEX|CONCURRENTLY", MatchAny, "ON") || else if (TailMatches3("INDEX|CONCURRENTLY", MatchAny, "ON") ||
TailMatches2("INDEX|CONCURRENTLY", "ON")) TailMatches2("INDEX|CONCURRENTLY", "ON"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tpm, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables, NULL);
/* /*
* Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing * Complete CREATE|UNIQUE INDEX CONCURRENTLY with "ON" and existing
...@@ -2903,8 +2914,7 @@ psql_completion(const char *text, int start, int end) ...@@ -2903,8 +2914,7 @@ psql_completion(const char *text, int start, int end)
} }
/* /*
* Complete GRANT/REVOKE <sth> ON with a list of tables, views, and * Complete GRANT/REVOKE <sth> ON with a list of appropriate relations.
* sequences.
* *
* Keywords like DATABASE, FUNCTION, LANGUAGE and SCHEMA added to query * Keywords like DATABASE, FUNCTION, LANGUAGE and SCHEMA added to query
* result via UNION; seems to work intuitively. * result via UNION; seems to work intuitively.
...@@ -2922,7 +2932,7 @@ psql_completion(const char *text, int start, int end) ...@@ -2922,7 +2932,7 @@ psql_completion(const char *text, int start, int end)
if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES")) if (HeadMatches3("ALTER", "DEFAULT", "PRIVILEGES"))
COMPLETE_WITH_LIST7("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS"); COMPLETE_WITH_LIST7("TABLES", "SEQUENCES", "FUNCTIONS", "PROCEDURES", "ROUTINES", "TYPES", "SCHEMAS");
else else
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables,
" UNION SELECT 'ALL FUNCTIONS IN SCHEMA'" " UNION SELECT 'ALL FUNCTIONS IN SCHEMA'"
" UNION SELECT 'ALL PROCEDURES IN SCHEMA'" " UNION SELECT 'ALL PROCEDURES IN SCHEMA'"
" UNION SELECT 'ALL ROUTINES IN SCHEMA'" " UNION SELECT 'ALL ROUTINES IN SCHEMA'"
...@@ -2977,7 +2987,7 @@ psql_completion(const char *text, int start, int end) ...@@ -2977,7 +2987,7 @@ psql_completion(const char *text, int start, int end)
else if (TailMatches1("SEQUENCE")) else if (TailMatches1("SEQUENCE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
else if (TailMatches1("TABLE")) else if (TailMatches1("TABLE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables, NULL);
else if (TailMatches1("TABLESPACE")) else if (TailMatches1("TABLESPACE"))
COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces); COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
else if (TailMatches1("TYPE")) else if (TailMatches1("TYPE"))
...@@ -3180,7 +3190,7 @@ psql_completion(const char *text, int start, int end) ...@@ -3180,7 +3190,7 @@ psql_completion(const char *text, int start, int end)
else if (Matches1("REINDEX")) else if (Matches1("REINDEX"))
COMPLETE_WITH_LIST5("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE"); COMPLETE_WITH_LIST5("TABLE", "INDEX", "SYSTEM", "SCHEMA", "DATABASE");
else if (Matches2("REINDEX", "TABLE")) else if (Matches2("REINDEX", "TABLE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexables, NULL);
else if (Matches2("REINDEX", "INDEX")) else if (Matches2("REINDEX", "INDEX"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
else if (Matches2("REINDEX", "SCHEMA")) else if (Matches2("REINDEX", "SCHEMA"))
...@@ -3327,7 +3337,7 @@ psql_completion(const char *text, int start, int end) ...@@ -3327,7 +3337,7 @@ psql_completion(const char *text, int start, int end)
/* TABLE, but not TABLE embedded in other commands */ /* TABLE, but not TABLE embedded in other commands */
else if (Matches1("TABLE")) else if (Matches1("TABLE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables, NULL);
/* TABLESAMPLE */ /* TABLESAMPLE */
else if (TailMatches1("TABLESAMPLE")) else if (TailMatches1("TABLESAMPLE"))
...@@ -3377,29 +3387,29 @@ psql_completion(const char *text, int start, int end) ...@@ -3377,29 +3387,29 @@ psql_completion(const char *text, int start, int end)
* VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ] * VACUUM [ FULL | FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
*/ */
else if (Matches1("VACUUM")) else if (Matches1("VACUUM"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'FULL'" " UNION SELECT 'FULL'"
" UNION SELECT 'FREEZE'" " UNION SELECT 'FREEZE'"
" UNION SELECT 'ANALYZE'" " UNION SELECT 'ANALYZE'"
" UNION SELECT 'VERBOSE'"); " UNION SELECT 'VERBOSE'");
else if (Matches2("VACUUM", "FULL|FREEZE")) else if (Matches2("VACUUM", "FULL|FREEZE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'ANALYZE'" " UNION SELECT 'ANALYZE'"
" UNION SELECT 'VERBOSE'"); " UNION SELECT 'VERBOSE'");
else if (Matches3("VACUUM", "FULL|FREEZE", "ANALYZE")) else if (Matches3("VACUUM", "FULL|FREEZE", "ANALYZE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'VERBOSE'"); " UNION SELECT 'VERBOSE'");
else if (Matches3("VACUUM", "FULL|FREEZE", "VERBOSE")) else if (Matches3("VACUUM", "FULL|FREEZE", "VERBOSE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'ANALYZE'"); " UNION SELECT 'ANALYZE'");
else if (Matches2("VACUUM", "VERBOSE")) else if (Matches2("VACUUM", "VERBOSE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'ANALYZE'"); " UNION SELECT 'ANALYZE'");
else if (Matches2("VACUUM", "ANALYZE")) else if (Matches2("VACUUM", "ANALYZE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables,
" UNION SELECT 'VERBOSE'"); " UNION SELECT 'VERBOSE'");
else if (HeadMatches1("VACUUM")) else if (HeadMatches1("VACUUM"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_vacuumables, NULL);
/* WITH [RECURSIVE] */ /* WITH [RECURSIVE] */
...@@ -3411,9 +3421,9 @@ psql_completion(const char *text, int start, int end) ...@@ -3411,9 +3421,9 @@ psql_completion(const char *text, int start, int end)
COMPLETE_WITH_CONST("RECURSIVE"); COMPLETE_WITH_CONST("RECURSIVE");
/* ANALYZE */ /* ANALYZE */
/* Complete with list of tables */ /* Complete with list of appropriate relations */
else if (Matches1("ANALYZE")) else if (Matches1("ANALYZE"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_analyzables, NULL);
/* WHERE */ /* WHERE */
/* Simple case of the word before the where being the table name */ /* Simple case of the word before the where being the table name */
...@@ -3423,11 +3433,11 @@ psql_completion(const char *text, int start, int end) ...@@ -3423,11 +3433,11 @@ psql_completion(const char *text, int start, int end)
/* ... FROM ... */ /* ... FROM ... */
/* TODO: also include SRF ? */ /* TODO: also include SRF ? */
else if (TailMatches1("FROM") && !Matches3("COPY|\\copy", MatchAny, "FROM")) else if (TailMatches1("FROM") && !Matches3("COPY|\\copy", MatchAny, "FROM"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables, NULL);
/* ... JOIN ... */ /* ... JOIN ... */
else if (TailMatches1("JOIN")) else if (TailMatches1("JOIN"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_selectables, NULL);
/* Backslash commands */ /* Backslash commands */
/* TODO: \dc \dd \dl */ /* TODO: \dc \dd \dl */
...@@ -3477,7 +3487,7 @@ psql_completion(const char *text, int start, int end) ...@@ -3477,7 +3487,7 @@ psql_completion(const char *text, int start, int end)
else if (TailMatchesCS1("\\dn*")) else if (TailMatchesCS1("\\dn*"))
COMPLETE_WITH_QUERY(Query_for_list_of_schemas); COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
else if (TailMatchesCS1("\\dp") || TailMatchesCS1("\\z")) else if (TailMatchesCS1("\\dp") || TailMatchesCS1("\\z"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_grantables, NULL);
else if (TailMatchesCS1("\\ds*")) else if (TailMatchesCS1("\\ds*"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL); COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
else if (TailMatchesCS1("\\dt*")) else if (TailMatchesCS1("\\dt*"))
......
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