8000 Fix tab completion in psql for ALTER DEFAULT PRIVILEGES · hackingwu/postgres@26b55d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 26b55d6

Browse files
committed
Fix tab completion in psql for ALTER DEFAULT PRIVILEGES
When providing tab completion for ALTER DEFAULT PRIVILEGES, we are including the list of roles as possible options for completion after the GRANT or REVOKE. Further, we accept FOR ROLE/IN SCHEMA at the same time and in either order, but the tab completion was only working for one or the other. Lastly, we weren't using the actual list of allowed kinds of objects for default privileges for completion after the 'GRANT X ON' but instead were completeing to what 'GRANT X ON' supports, which isn't the ssame at all. Address these issues by improving the forward tab-completion for ALTER DEFAULT PRIVILEGES and then constrain and correct how the tail completion is done when it is for ALTER DEFAULT PRIVILEGES. Back-patch the forward/tail tab-completion to 9.6, where we made it easy to handle such cases. For 9.5 and earlier, correct the initial tab-completion to at least be correct as far as it goes and then add a check for GRANT/REVOKE to only tab-complete when the GRANT/REVOKE is the start of the command, so we don't try to do tab-completion after we get to the GRANT/REVOKE part of the ALTER DEFAULT PRIVILEGES command, which is better than providing incorrect completions. Initial patch for master and 9.6 by Gilles Darold, though I cleaned it up and added a few comments. All bugs in the 9.5 and earlier patch are mine. Discussion: https://www.postgresql.org/message-id/1614593c-e356-5b27-6dba-66320a9bc68b@dalibo.com
1 parent de651a6 commit 26b55d6

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/bin/psql/tab-complete.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ psql_completion(char *text, int start, int end)
10591059
pg_strcasecmp(prev_wd, "PRIVILEGES") == 0)
10601060
{
10611061
static const char *const list_ALTER_DEFAULT_PRIVILEGES[] =
1062-
{"FOR ROLE", "FOR USER", "IN SCHEMA", NULL};
1062+
{"FOR ROLE", "IN SCHEMA", NULL};
10631063

10641064
COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES);
10651065
}
@@ -1069,19 +1069,26 @@ psql_completion(char *text, int start, int end)
10691069
pg_strcasecmp(prev2_wd, "PRIVILEGES") == 0 &&
10701070
pg_strcasecmp(prev_wd, "FOR") == 0)
10711071
{
1072-
static const char *const list_ALTER_DEFAULT_PRIVILEGES_FOR[] =
1073-
{"ROLE", "USER", NULL};
1072+
COMPLETE_WITH_CONST("ROLE");
1073+
}
1074+
/* ALTER DEFAULT PRIVILEGES FOR ROLE ... } */
1075+
else if (pg_strcasecmp(prev5_wd, "DEFAULT") == 0 &&
1076+
pg_strcasecmp(prev4_wd, "PRIVILEGES") == 0 &&
1077+
pg_strcasecmp(prev3_wd, "FOR") == 0)
1078+
{
1079+
static const char *const list_ALTER_DEFAULT_PRIVILEGES_REST[] =
1080+
{"GRANT", "REVOKE", "IN SCHEMA", NULL};
10741081

1075-
COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_FOR);
1082+
COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_REST);
10761083
}
1077-
/* ALTER DEFAULT PRIVILEGES { FOR ROLE ... | IN SCHEMA ... } */
1084+
/* ALTER DEFAULT PRIVILEGES IN SCHEMA ... } */
10781085
else if (pg_strcasecmp(prev5_wd, "DEFAULT") == 0 &&
10791086
pg_strcasecmp(prev4_wd, "PRIVILEGES") == 0 &&
1080-
(pg_strcasecmp(prev3_wd, "FOR") == 0 ||
1081-
pg_strcasecmp(prev3_wd, "IN") == 0))
1087+
pg_strcasecmp(prev3_wd, "IN") == 0 &&
1088+
pg_strcasecmp(prev2_wd, "SCHEMA") == 0)
10821089
{
10831090
static const char *const list_ALTER_DEFAULT_PRIVILEGES_REST[] =
1084-
{"GRANT", "REVOKE", NULL};
1091+
{"GRANT", "REVOKE", "FOR ROLE", NULL};
10851092

10861093
COMPLETE_WITH_LIST(list_ALTER_DEFAULT_PRIVILEGES_REST);
10871094
}
@@ -2299,8 +2306,9 @@ psql_completion(char *text, int start, int end)
22992306

23002307
/* GRANT && REVOKE */
23012308
/* Complete GRANT/REVOKE with a list of roles and privileges */
2302-
else if (pg_strcasecmp(prev_wd, "GRANT") == 0 ||
2303-
pg_strcasecmp(prev_wd, "REVOKE") == 0)
2309+
else if (prev2_wd[0] == '\0' &&
2310+
(pg_strcasecmp(prev_wd, "GRANT") == 0 ||
2311+
pg_strcasecmp(prev_wd, "REVOKE") == 0))
23042312
{
23052313
COMPLETE_WITH_QUERY(Query_for_list_of_roles
23062314
" UNION SELECT 'SELECT'"

0 commit comments

Comments
 (0)
0