Add more tab completion support for ALTER DEFAULT PRIVILEGES in psql.
authorMasahiko Sawada <msawada@postgresql.org>
Mon, 8 Apr 2024 03:15:10 +0000 (12:15 +0900)
committerMasahiko Sawada <msawada@postgresql.org>
Mon, 8 Apr 2024 03:15:10 +0000 (12:15 +0900)
This adds tab completion of "GRANT" and "REVOKE [GRANT OPTION FOR]"
for ALTER DEFAULT PRIVILEGES, and adds "WITH GRANT OPTION" for
ALTER DEFAULT PRIVILEGES ... GRANT ... TO role.

Author: Vignesh C, with cosmetic adjustments by me
Reviewed-by: Shubham Khanna, Masahiko Sawada
Discussion: https://postgr.es/m/CALDaNm1aEdJb-QJi%3DGWStkfj_%2BEDUK_VtDkn%2BTjQ2z7HyU0MBw%40mail.gmail.com

src/bin/psql/tab-complete.c

index 82eb3955abf5a9ab0fb6e5d76085a07be4553912..6fee3160f029f22c70ee9161cafc130d0903f9c3 100644 (file)
@@ -2147,7 +2147,7 @@ psql_completion(const char *text, int start, int end)
 
    /* ALTER DEFAULT PRIVILEGES */
    else if (Matches("ALTER", "DEFAULT", "PRIVILEGES"))
-       COMPLETE_WITH("FOR ROLE", "IN SCHEMA");
+       COMPLETE_WITH("FOR", "GRANT", "IN SCHEMA", "REVOKE");
    /* ALTER DEFAULT PRIVILEGES FOR */
    else if (Matches("ALTER", "DEFAULT", "PRIVILEGES", "FOR"))
        COMPLETE_WITH("ROLE");
@@ -3949,9 +3949,18 @@ psql_completion(const char *text, int start, int end)
         * privileges (can't grant roles)
         */
        if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES"))
-           COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
-                         "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
-                         "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
+       {
+           if (TailMatches("GRANT") ||
+               TailMatches("REVOKE", "GRANT", "OPTION", "FOR"))
+               COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
+                             "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
+                             "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL");
+           else if (TailMatches("REVOKE"))
+               COMPLETE_WITH("SELECT", "INSERT", "UPDATE",
+                             "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER",
+                             "CREATE", "EXECUTE", "USAGE", "MAINTAIN", "ALL",
+                             "GRANT OPTION FOR");
+       }
        else if (TailMatches("GRANT"))
            COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
                                     Privilege_options_of_grant_and_revoke);
@@ -4133,6 +4142,9 @@ psql_completion(const char *text, int start, int end)
    else if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES") && TailMatches("TO|FROM"))
        COMPLETE_WITH_QUERY_PLUS(Query_for_list_of_roles,
                                 Keywords_for_list_of_grant_roles);
+   /* Offer WITH GRANT OPTION after that */
+   else if (HeadMatches("ALTER", "DEFAULT", "PRIVILEGES") && TailMatches("TO", MatchAny))
+       COMPLETE_WITH("WITH GRANT OPTION");
    /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */
    else if (HeadMatches("GRANT") && TailMatches("ON", MatchAny, MatchAny))
        COMPLETE_WITH("TO");