]> granicus.if.org Git - postgresql/commitdiff
psql: Add support for tab completion of GRANT/REVOKE role
authorPeter Eisentraut <peter_e@gmx.net>
Sat, 21 Jan 2012 17:46:55 +0000 (19:46 +0200)
committerPeter Eisentraut <peter_e@gmx.net>
Sat, 21 Jan 2012 17:46:55 +0000 (19:46 +0200)
Previously, only GRANT/REVOKE privilege was supported.

reviewed by Pavel Stehule

src/bin/psql/tab-complete.c

index a27ef699f6a1245a54a3db369946b0feeb5a7a4b..6efc0ce0c0f78558b5ef3a1bd0c821e36399c96d 100644 (file)
@@ -2209,21 +2209,52 @@ psql_completion(char *text, int start, int end)
                COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_foreign_tables, NULL);
 
 /* GRANT && REVOKE */
-       /* Complete GRANT/REVOKE with a list of privileges */
+       /* Complete GRANT/REVOKE with a list of roles and privileges */
        else if (pg_strcasecmp(prev_wd, "GRANT") == 0 ||
                         pg_strcasecmp(prev_wd, "REVOKE") == 0)
        {
-               static const char *const list_privilege[] =
-               {"SELECT", "INSERT", "UPDATE", "DELETE", "TRUNCATE", "REFERENCES",
-                       "TRIGGER", "CREATE", "CONNECT", "TEMPORARY", "EXECUTE", "USAGE",
-               "ALL", NULL};
-
-               COMPLETE_WITH_LIST(list_privilege);
-       }
-       /* Complete GRANT/REVOKE <sth> with "ON" */
+               COMPLETE_WITH_QUERY(Query_for_list_of_roles
+                                                       " UNION SELECT 'SELECT'"
+                                                       " UNION SELECT 'INSERT'"
+                                                       " UNION SELECT 'UPDATE'"
+                                                       " UNION SELECT 'DELETE'"
+                                                       " UNION SELECT 'TRUNCATE'"
+                                                       " UNION SELECT 'REFERENCES'"
+                                                       " UNION SELECT 'TRIGGER'"
+                                                       " UNION SELECT 'CREATE'"
+                                                       " UNION SELECT 'CONNECT'"
+                                                       " UNION SELECT 'TEMPORARY'"
+                                                       " UNION SELECT 'EXECUTE'"
+                                                       " UNION SELECT 'USAGE'"
+                                                       " UNION SELECT 'ALL'");
+       }
+       /* Complete GRANT/REVOKE <privilege> with "ON", GRANT/REVOKE <role> with TO/FROM */
        else if (pg_strcasecmp(prev2_wd, "GRANT") == 0 ||
                         pg_strcasecmp(prev2_wd, "REVOKE") == 0)
-               COMPLETE_WITH_CONST("ON");
+       {
+               if (pg_strcasecmp(prev_wd, "SELECT") == 0
+                       || pg_strcasecmp(prev_wd, "INSERT") == 0
+                       || pg_strcasecmp(prev_wd, "UPDATE") == 0
+                       || pg_strcasecmp(prev_wd, "DELETE") == 0
+                       || pg_strcasecmp(prev_wd, "TRUNCATE") == 0
+                       || pg_strcasecmp(prev_wd, "REFERENCES") == 0
+                       || pg_strcasecmp(prev_wd, "TRIGGER") == 0
+                       || pg_strcasecmp(prev_wd, "CREATE") == 0
+                       || pg_strcasecmp(prev_wd, "CONNECT") == 0
+                       || pg_strcasecmp(prev_wd, "TEMPORARY") == 0
+                       || pg_strcasecmp(prev_wd, "TEMP") == 0
+                       || pg_strcasecmp(prev_wd, "EXECUTE") == 0
+                       || pg_strcasecmp(prev_wd, "USAGE") == 0
+                       || pg_strcasecmp(prev_wd, "ALL") == 0)
+                       COMPLETE_WITH_CONST("ON");
+               else
+               {
+                       if (pg_strcasecmp(prev2_wd, "GRANT") == 0)
+                               COMPLETE_WITH_CONST("TO");
+                       else
+                               COMPLETE_WITH_CONST("FROM");
+               }
+       }
 
        /*
         * Complete GRANT/REVOKE <sth> ON with a list of tables, views, sequences,
@@ -2304,6 +2335,18 @@ psql_completion(char *text, int start, int end)
                        COMPLETE_WITH_CONST("FROM");
        }
 
+       /* Complete "GRANT/REVOKE * TO/FROM" with username, GROUP, or PUBLIC */
+       else if (pg_strcasecmp(prev3_wd, "GRANT") == 0 &&
+                        pg_strcasecmp(prev_wd, "TO") == 0)
+       {
+               COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
+       }
+       else if (pg_strcasecmp(prev3_wd, "REVOKE") == 0 &&
+                        pg_strcasecmp(prev_wd, "FROM") == 0)
+       {
+               COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles);
+       }
+
 /* GROUP BY */
        else if (pg_strcasecmp(prev3_wd, "FROM") == 0 &&
                         pg_strcasecmp(prev_wd, "GROUP") == 0)