Skip to content

Commit 07f7237

Browse files
committed
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
1 parent 967db24 commit 07f7237

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/bin/psql/tab-complete.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,12 @@ static const SchemaQuery Query_for_trigger_of_table = {
11431143
" FROM pg_catalog.pg_timezone_names() "\
11441144
" WHERE pg_catalog.quote_literal(pg_catalog.lower(name)) LIKE pg_catalog.lower('%s')"
11451145

1146+
/* Privilege options shared between GRANT and REVOKE */
1147+
#define Privilege_options_of_grant_and_revoke \
1148+
"SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", \
1149+
"CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE", "SET", "ALTER SYSTEM", \
1150+
"ALL"
1151+
11461152
/*
11471153
* These object types were introduced later than our support cutoff of
11481154
* server version 9.2. We use the VersionedQuery infrastructure so that
@@ -3767,7 +3773,7 @@ psql_completion(const char *text, int start, int end)
37673773
*/
37683774
/* Complete GRANT/REVOKE with a list of roles and privileges */
37693775
else if (TailMatches("GRANT|REVOKE") ||
3770-
TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
3776+
TailMatches("REVOKE", "ADMIN|GRANT|INHERIT", "OPTION", "FOR"))
37713777
{
37723778
/*
37733779
* With ALTER DEFAULT PRIVILEGES, restrict completion to grantable
@@ -3776,32 +3782,22 @@ psql_completion(const char *text, int start, int end)
37763782
if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
37773783
COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
37783784
"DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
3779-
"EXECUTE", "USAGE", "ALL");
3780-
else
3785+
"CREATE", "EXECUTE", "USAGE", "ALL");
3786+
else if (TailMatches("GRANT"))
3787+
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3788+
Privilege_options_of_grant_and_revoke);
3789+
else if (TailMatches("REVOKE"))
37813790
COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
3782-
"GRANT",
3783-
"SELECT",
3784-
"INSERT",
3785-
"UPDATE",
3786-
"DELETE",
3787-
"TRUNCATE",
3788-
"REFERENCES",
3789-
"TRIGGER",
3790-
"CREATE",
3791-
"CONNECT",
3792-
"TEMPORARY",
3793-
"EXECUTE",
3794-
"USAGE",
3795-
"SET",
3796-
"ALTER SYSTEM",
3797-
"ALL");
3791+
Privilege_options_of_grant_and_revoke,
3792+
"GRANT OPTION FOR",
3793+
"ADMIN OPTION FOR",
3794+
"INHERIT OPTION FOR");
3795+
else if (TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
3796+
COMPLETE_WITH(Privilege_options_of_grant_and_revoke);
3797+
else if (TailMatches("REVOKE", "ADMIN|INHERIT", "OPTION", "FOR"))
3798+
COMPLETE_WITH_QUERY(Query_for_list_of_roles);
37983799
}
37993800

3800-
else if (TailMatches("REVOKE", "GRANT"))
3801-
COMPLETE_WITH("OPTION FOR");
3802-
else if (TailMatches("REVOKE", "GRANT", "OPTION"))
3803-
COMPLETE_WITH("FOR");
3804-
38053801
else if (TailMatches("GRANT|REVOKE", "ALTER") ||
38063802
TailMatches("REVOKE", "GRANT", "OPTION", "FOR", "ALTER"))
38073803
COMPLETE_WITH("SYSTEM");
@@ -3943,12 +3939,17 @@ psql_completion(const char *text, int start, int end)
39433939
* Offer grant options after that.
39443940
*/
39453941
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny))
3946-
COMPLETE_WITH("WITH ADMIN OPTION",
3942+
COMPLETE_WITH("WITH ADMIN",
3943+
"WITH INHERIT",
39473944
"WITH GRANT OPTION",
39483945
"GRANTED BY");
39493946
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH"))
3950-
COMPLETE_WITH("ADMIN OPTION",
3947+
COMPLETE_WITH("ADMIN",
3948+
"INHERIT",
39513949
"GRANT OPTION");
3950+
else if (HeadMatches("GRANT") &&
3951+
(TailMatches("TO", MatchAny, "WITH", "ADMIN|INHERIT")))
3952+
COMPLETE_WITH("OPTION", "TRUE", "FALSE");
39523953
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION"))
39533954
COMPLETE_WITH("GRANTED BY");
39543955
else if (HeadMatches("GRANT") && TailMatches("TO", MatchAny, "WITH", MatchAny, "OPTION", "GRANTED", "BY"))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy