psql: Improve tab completion for GRANT/REVOKE

This commit improves the handling of the following clauses:
- Addition of "CREATE" for ALTER DEFAULT PRIVILEGES .. GRANT/REVOKE.
- Addition of GRANT|ADMIN|INHERIT OPTION FOR for REVOKE, with some
completion for roles, INHERIT being added recently by e3ce2de.
- Addition of GRANT WITH ADMIN|INHERIT.

The list of privilege options common to GRANT and REVOKE is refactored
to avoid its duplication.

Author: Shi Yu
Reviewed-by: Kyotaro Horiguchi, Michael Paquier, Peter Smith
Discussion: https://postgr.es/m/OSZPR01MB6310FCE8609185A56344EED2FD559@OSZPR01MB6310.jpnprd01.prod.outlook.com
This commit is contained in:
Michael Paquier 2022-11-18 11:26:49 +09:00
parent 967db242c2
commit 07f7237c2a

View File

@ -1143,6 +1143,12 @@ static const SchemaQuery Query_for_trigger_of_table = {
" FROM pg_catalog.pg_timezone_names() "\ " FROM pg_catalog.pg_timezone_names() "\
" WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')" " WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
/* Privilege options shared between GRANT and REVOKE */
#define Privilege_options_of_grant_and_revoke \
"SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
"CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
"ALL"
/* /*
* These object types were introduced later than our support cutoff of * These object types were introduced later than our support cutoff of
* server version 9.2. We use the VersionedQuery infrastructure so that * server version 9.2. We use the VersionedQuery infrastructure so that
@ -3767,7 +3773,7 @@ psql_completion(const char *text, int start, int end)
*/ */
/* Complete GRANT/REVOKE with a list of roles and privileges */ /* Complete GRANT/REVOKE with a list of roles and privileges */
else if (TailMatches("GRANT|REVOKE") || else if (TailMatches("GRANT|REVOKE") ||
TailMatches("REVOKE", "GRANT", "OPTION", "FOR")) TailMatches("REVOKE", "ADMIN|GRANT|INHERIT", "OPTION", "FOR"))
{ {
/* /*
* With ALTER DEFAULT PRIVILEGES, restrict completion to grantable * With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
@ -3776,32 +3782,22 @@ psql_completion(const char *text, int start, int end)
if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES")) if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
COMPLETE_WITH("SELECT", "INSERT", "UPDATE", COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
"DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
"EXECUTE", "USAGE", "ALL"); "CREATE", "EXECUTE", "USAGE", "ALL");
else else if (TailMatches("GRANT"))
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles, COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
"GRANT", Privilege_options_of_grant_and_revoke);
"SELECT", else if (TailMatches("REVOKE"))
"INSERT", COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
"UPDATE", Privilege_options_of_grant_and_revoke,
"DELETE", "GRANT OPTION FOR",
"TRUNCATE", "ADMIN OPTION FOR",
"REFERENCES", "INHERIT OPTION FOR");
"TRIGGER", else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
"CREATE", COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
"CONNECT", else if (TailMatches("REVOKE", "ADMIN|INHERIT", "OPTION", "FOR"))
"TEMPORARY", COMPLETE_WITH_QUERY(Query_for_list_of_roles);
"EXECUTE",
"USAGE",
"SET",
"ALTER SYSTEM",
"ALL");
} }
else if (TailMatches("REVOKE", "GRANT"))
COMPLETE_WITH("OPTION FOR");
else if (TailMatches("REVOKE", "GRANT", "OPTION"))
COMPLETE_WITH("FOR");
else if (TailMatches("GRANT|REVOKE", "ALTER") || else if (TailMatches("GRANT|REVOKE", "ALTER") ||
TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER")) TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
COMPLETE_WITH("SYSTEM"); COMPLETE_WITH("SYSTEM");
@ -3943,12 +3939,17 @@ psql_completion(const char *text, int start, int end)
* Offer grant options after that. * Offer grant options after that.
*/ */
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny)) else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny))
COMPLETE_WITH("WITH ADMIN OPTION", COMPLETE_WITH("WITH ADMIN",
"WITH INHERIT",
"WITH GRANT OPTION", "WITH GRANT OPTION",
"GRANTED BY"); "GRANTED BY");
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH")) else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH"))
COMPLETE_WITH("ADMIN OPTION", COMPLETE_WITH("ADMIN",
"INHERIT",
"GRANT OPTION"); "GRANT OPTION");
else if (HeadMatches("GRANT") &&
(TailMatches("TO", MatchAny, "WITH", "ADMIN|INHERIT")))
COMPLETE_WITH("OPTION", "TRUE", "FALSE");
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION")) else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION"))
COMPLETE_WITH("GRANTED BY"); COMPLETE_WITH("GRANTED BY");
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY")) else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))