Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf3fd531

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

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

‎src/bin/psql/tab-complete.c

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,13 +1570,31 @@ psql_completion(const char *text, int start, int end)
15701570
COMPLETE_WITH_CONST("PASSWORD");
15711571
/* ALTER DEFAULT PRIVILEGES */
15721572
elseif (Matches3("ALTER","DEFAULT","PRIVILEGES"))
1573-
COMPLETE_WITH_LIST3("FOR ROLE","FOR USER","IN SCHEMA");
1573+
COMPLETE_WITH_LIST2("FOR ROLE","IN SCHEMA");
15741574
/* ALTER DEFAULT PRIVILEGES FOR */
15751575
elseif (Matches4("ALTER","DEFAULT","PRIVILEGES","FOR"))
1576-
COMPLETE_WITH_LIST2("ROLE","USER");
1577-
/* ALTER DEFAULT PRIVILEGES { FOR ROLE ... | IN SCHEMA ... } */
1578-
elseif (Matches6("ALTER","DEFAULT","PRIVILEGES","FOR","ROLE|USER",MatchAny)||
1579-
Matches6("ALTER","DEFAULT","PRIVILEGES","IN","SCHEMA",MatchAny))
1576+
COMPLETE_WITH_CONST("ROLE");
1577+
/* ALTER DEFAULT PRIVILEGES IN */
1578+
elseif (Matches4("ALTER","DEFAULT","PRIVILEGES","IN"))
1579+
COMPLETE_WITH_CONST("SCHEMA");
1580+
/* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... */
1581+
elseif (Matches6("ALTER","DEFAULT","PRIVILEGES","FOR","ROLE|USER",
1582+
MatchAny))
1583+
COMPLETE_WITH_LIST3("GRANT","REVOKE","IN SCHEMA");
1584+
/* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */
1585+
elseif (Matches6("ALTER","DEFAULT","PRIVILEGES","IN","SCHEMA",
1586+
MatchAny))
1587+
COMPLETE_WITH_LIST3("GRANT","REVOKE","FOR ROLE");
1588+
/* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR */
1589+
elseif (Matches7("ALTER","DEFAULT","PRIVILEGES","IN","SCHEMA",
1590+
MatchAny,"FOR"))
1591+
COMPLETE_WITH_CONST("ROLE");
1592+
/* ALTER DEFAULT PRIVILEGES FOR ROLE|USER ... IN SCHEMA ... */
1593+
/* ALTER DEFAULT PRIVILEGES IN SCHEMA ... FOR ROLE|USER ... */
1594+
elseif (Matches9("ALTER","DEFAULT","PRIVILEGES","FOR","ROLE|USER",
1595+
MatchAny,"IN","SCHEMA",MatchAny)||
1596+
Matches9("ALTER","DEFAULT","PRIVILEGES","IN","SCHEMA",
1597+
MatchAny,"FOR","ROLE|USER",MatchAny))
15801598
COMPLETE_WITH_LIST2("GRANT","REVOKE");
15811599
/* ALTER DOMAIN <name> */
15821600
elseif (Matches3("ALTER","DOMAIN",MatchAny))
@@ -2566,10 +2584,22 @@ psql_completion(const char *text, int start, int end)
25662584
elseif (TailMatches2("FOREIGN","SERVER"))
25672585
COMPLETE_WITH_QUERY(Query_for_list_of_servers);
25682586

2569-
/* GRANT && REVOKE --- is allowed inside CREATE SCHEMA, so use TailMatches */
2587+
/*
2588+
* GRANT and REVOKE are allowed inside CREATE SCHEMA and
2589+
* ALTER DEFAULT PRIVILEGES, so use TailMatches
2590+
*/
25702591
/* Complete GRANT/REVOKE with a list of roles and privileges */
25712592
elseif (TailMatches1("GRANT|REVOKE"))
2572-
COMPLETE_WITH_QUERY(Query_for_list_of_roles
2593+
/*
2594+
* With ALTER DEFAULT PRIVILEGES, restrict completion
2595+
* to grantable privileges (can't grant roles)
2596+
*/
2597+
if (HeadMatches3("ALTER","DEFAULT","PRIVILEGES"))
2598+
COMPLETE_WITH_LIST10("SELECT","INSERT","UPDATE",
2599+
"DELETE","TRUNCATE","REFERENCES","TRIGGER",
2600+
"EXECUTE","USAGE","ALL");
2601+
else
2602+
COMPLETE_WITH_QUERY(Query_for_list_of_roles
25732603
" UNION SELECT 'SELECT'"
25742604
" UNION SELECT 'INSERT'"
25752605
" UNION SELECT 'UPDATE'"
@@ -2610,7 +2640,14 @@ psql_completion(const char *text, int start, int end)
26102640
* privilege.
26112641
*/
26122642
elseif (TailMatches3("GRANT|REVOKE",MatchAny,"ON"))
2613-
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf,
2643+
/*
2644+
* With ALTER DEFAULT PRIVILEGES, restrict completion
2645+
* to the kinds of objects supported.
2646+
*/
2647+
if (HeadMatches3("ALTER","DEFAULT","PRIVILEGES"))
2648+
COMPLETE_WITH_LIST4("TABLES","SEQUENCES","FUNCTIONS","TYPES");
2649+
else
2650+
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf,
26142651
" UNION SELECT 'ALL FUNCTIONS IN SCHEMA'"
26152652
" UNION SELECT 'ALL SEQUENCES IN SCHEMA'"
26162653
" UNION SELECT 'ALL TABLES IN SCHEMA'"
@@ -2673,7 +2710,9 @@ psql_completion(const char *text, int start, int end)
26732710
elseif ((HeadMatches1("GRANT")&&TailMatches1("TO"))||
26742711
(HeadMatches1("REVOKE")&&TailMatches1("FROM")))
26752712
COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
2676-
2713+
/* Complete "ALTER DEFAULT PRIVILEGES ... GRANT/REVOKE ... TO/FROM */
2714+
elseif (HeadMatches3("ALTER","DEFAULT","PRIVILEGES")&&TailMatches1("TO|FROM"))
2715+
COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
26772716
/* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
26782717
elseif (HeadMatches1("GRANT")&&TailMatches3("ON",MatchAny,MatchAny))
26792718
COMPLETE_WITH_CONST("TO");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp