Skip to content

Commit 95c63b5

Browse files
committed
psql: Add support for tab completion of GRANT/REVOKE role
Previously, only GRANT/REVOKE privilege was supported. reviewed by Pavel Stehule
1 parent c145349 commit 95c63b5

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

src/bin/psql/tab-complete.c

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,21 +2209,52 @@ psql_completion(char *text, int start, int end)
22092209
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL);
22102210

22112211
/* GRANT && REVOKE */
2212-
/* Complete GRANT/REVOKE with a list of privileges */
2212+
/* Complete GRANT/REVOKE with a list of roles and privileges */
22132213
else if (pg_strcasecmp(prev_wd, "GRANT") == 0 ||
22142214
pg_strcasecmp(prev_wd, "REVOKE") == 0)
22152215
{
2216-
static const char *const list_privilege[] =
2217-
{"SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES",
2218-
"TRIGGER", "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE",
2219-
"ALL", NULL};
2220-
2221-
COMPLETE_WITH_LIST(list_privilege);
2222-
}
2223-
/* Complete GRANT/REVOKE <sth> with "ON" */
2216+
COMPLETE_WITH_QUERY(Query_for_list_of_roles
2217+
" UNION SELECT 'SELECT'"
2218+
" UNION SELECT 'INSERT'"
2219+
" UNION SELECT 'UPDATE'"
2220+
" UNION SELECT 'DELETE'"
2221+
" UNION SELECT 'TRUNCATE'"
2222+
" UNION SELECT 'REFERENCES'"
2223+
" UNION SELECT 'TRIGGER'"
2224+
" UNION SELECT 'CREATE'"
2225+
" UNION SELECT 'CONNECT'"
2226+
" UNION SELECT 'TEMPORARY'"
2227+
" UNION SELECT 'EXECUTE'"
2228+
" UNION SELECT 'USAGE'"
2229+
" UNION SELECT 'ALL'");
2230+
}
2231+
/* Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with TO/FROM */
22242232
else if (pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
22252233
pg_strcasecmp(prev2_wd, "REVOKE") == 0)
2226-
COMPLETE_WITH_CONST("ON");
2234+
{
2235+
if (pg_strcasecmp(prev_wd, "SELECT") == 0
2236+
|| pg_strcasecmp(prev_wd, "INSERT") == 0
2237+
|| pg_strcasecmp(prev_wd, "UPDATE") == 0
2238+
|| pg_strcasecmp(prev_wd, "DELETE") == 0
2239+
|| pg_strcasecmp(prev_wd, "TRUNCATE") == 0
2240+
|| pg_strcasecmp(prev_wd, "REFERENCES") == 0
2241+
|| pg_strcasecmp(prev_wd, "TRIGGER") == 0
2242+
|| pg_strcasecmp(prev_wd, "CREATE") == 0
2243+
|| pg_strcasecmp(prev_wd, "CONNECT") == 0
2244+
|| pg_strcasecmp(prev_wd, "TEMPORARY") == 0
2245+
|| pg_strcasecmp(prev_wd, "TEMP") == 0
2246+
|| pg_strcasecmp(prev_wd, "EXECUTE") == 0
2247+
|| pg_strcasecmp(prev_wd, "USAGE") == 0
2248+
|| pg_strcasecmp(prev_wd, "ALL") == 0)
2249+
COMPLETE_WITH_CONST("ON");
2250+
else
2251+
{
2252+
if (pg_strcasecmp(prev2_wd, "GRANT") == 0)
2253+
COMPLETE_WITH_CONST("TO");
2254+
else
2255+
COMPLETE_WITH_CONST("FROM");
2256+
}
2257+
}
22272258

22282259
/*
22292260
* Complete GRANT/REVOKE <sth> ON with a list of tables, views, sequences,
@@ -2304,6 +2335,18 @@ psql_completion(char *text, int start, int end)
23042335
COMPLETE_WITH_CONST("FROM");
23052336
}
23062337

2338+
/* Complete "GRANT/REVOKE * TO/FROM" with username, GROUP, or PUBLIC */
2339+
else if (pg_strcasecmp(prev3_wd, "GRANT") == 0 &&
2340+
pg_strcasecmp(prev_wd, "TO") == 0)
2341+
{
2342+
COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
2343+
}
2344+
else if (pg_strcasecmp(prev3_wd, "REVOKE") == 0 &&
2345+
pg_strcasecmp(prev_wd, "FROM") == 0)
2346+
{
2347+
COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
2348+
}
2349+
23072350
/* GROUP BY */
23082351
else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
23092352
pg_strcasecmp(prev_wd, "GROUP") == 0)

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