Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c8TPN-0004UO-0S for pgsql-hackers@arkaria.postgresql.org; Sun, 20 Nov 2016 14:47:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c8TPL-00017c-RI for pgsql-hackers@arkaria.postgresql.org; Sun, 20 Nov 2016 14:47:51 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c8TNw-00081P-9I for pgsql-hackers@postgresql.org; Sun, 20 Nov 2016 14:46:24 +0000 Received: from mimolette.dalibo.net ([212.85.157.144] helo=mail.dalibo.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c8TNs-00052r-2y for pgsql-hackers@postgresql.org; Sun, 20 Nov 2016 14:46:22 +0000 Received: from [192.168.1.128] (AGrenoble-653-1-523-31.w92-129.abo.wanadoo.fr [92.129.139.31]) by mail.dalibo.com (Postfix) with ESMTPSA id 00B142C02EC for ; Sun, 20 Nov 2016 15:46:15 +0100 (CET) To: PostgreSQL Hackers From: Gilles Darold Subject: [patch] psql tab completion for ALTER DEFAULT PRIVILEGES Message-ID: <1614593c-e356-5b27-6dba-66320a9bc68b@dalibo.com> Date: Sun, 20 Nov 2016 15:46:14 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------44BB8EA2DDE37F779CC6A6EA" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --------------44BB8EA2DDE37F779CC6A6EA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Hi, When tab-completing after ALTER DEFAULT PRIVILEGES ... GRANT|REVOKE, currently psql injects completion from the GRANT|REVOKE order, rather than the one expected. A patch is attached. It adds the right completion to GRANT|REVOKE after ALTER DEFAULT PRIVILEGES and after FOR ROLE|USER + IN SCHEMA. If there's no objection I will add it to next commit fest. Best regards, -- Gilles Darold Consultant PostgreSQL http://dalibo.com - http://dalibo.org --------------44BB8EA2DDE37F779CC6A6EA Content-Type: text/x-diff; name="patch-completion-alter-default-privileges.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="patch-completion-alter-default-privileges.diff" diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index b556c00..d0c8eda 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1574,9 +1574,23 @@ psql_completion(const char *text, int start, int end) /* ALTER DEFAULT PRIVILEGES FOR */ else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "FOR")) COMPLETE_WITH_LIST2("ROLE", "USER"); - /* ALTER DEFAULT PRIVILEGES { FOR ROLE ... | IN SCHEMA ... } */ - else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", MatchAny) || - Matches6("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", MatchAny)) + else if (Matches4("ALTER", "DEFAULT", "PRIVILEGES", "IN")) + COMPLETE_WITH_CONST("SCHEMA"); + /* ALTER DEFAULT PRIVILEGES FOR ROLE ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny)) + COMPLETE_WITH_LIST3("GRANT", "REVOKE", "IN SCHEMA"); + /* ALTER DEFAULT PRIVILEGES IN SCHEMA ... */ + else if (Matches6("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny)) + COMPLETE_WITH_LIST4("GRANT", "REVOKE", "FOR ROLE", "FOR USER"); + else if (Matches7("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR")) + COMPLETE_WITH_LIST2("ROLE", "USER"); + else if (Matches9("ALTER", "DEFAULT", "PRIVILEGES", "FOR", "ROLE|USER", + MatchAny, "IN", "SCHEMA", MatchAny) || + Matches9("ALTER", "DEFAULT", "PRIVILEGES", "IN", "SCHEMA", + MatchAny, "FOR", "ROLE|USER", MatchAny)) COMPLETE_WITH_LIST2("GRANT", "REVOKE"); /* ALTER DOMAIN */ else if (Matches3("ALTER", "DOMAIN", MatchAny)) @@ -2541,10 +2555,18 @@ psql_completion(const char *text, int start, int end) else if (TailMatches2("FOREIGN", "SERVER")) COMPLETE_WITH_QUERY(Query_for_list_of_servers); -/* GRANT && REVOKE --- is allowed inside CREATE SCHEMA, so use TailMatches */ +/* GRANT && REVOKE --- is allowed inside CREATE SCHEMA and + ALTER DEFAULT PRIVILEGES, so use TailMatches */ /* Complete GRANT/REVOKE with a list of roles and privileges */ else if (TailMatches1("GRANT|REVOKE")) - COMPLETE_WITH_QUERY(Query_for_list_of_roles + /* With ALTER DEFAULT PRIVILEGES, restrict completion + to authorized keywords */ + if (HeadMatches3("ALTER","DEFAULT","PRIVILEGES")) + COMPLETE_WITH_LIST10("SELECT", "INSERT", "UPDATE", + "DELETE", "TRUNCATE", "REFERENCES", "TRIGGER", + "EXECUTE", "USAGE", "ALL"); + else + COMPLETE_WITH_QUERY(Query_for_list_of_roles " UNION SELECT 'SELECT'" " UNION SELECT 'INSERT'" " UNION SELECT 'UPDATE'" @@ -2585,7 +2607,12 @@ psql_completion(const char *text, int start, int end) * privilege. */ else if (TailMatches3("GRANT|REVOKE", MatchAny, "ON")) - COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, + /* With ALTER DEFAULT PRIVILEGES, restrict completion + to authorized keywords */ + if (HeadMatches3("ALTER","DEFAULT","PRIVILEGES")) + COMPLETE_WITH_LIST4("TABLES", "SEQUENCES", "FUNCTIONS", "TYPES"); + else + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsvmf, " UNION SELECT 'ALL FUNCTIONS IN SCHEMA'" " UNION SELECT 'ALL SEQUENCES IN SCHEMA'" " UNION SELECT 'ALL TABLES IN SCHEMA'" @@ -2648,7 +2675,8 @@ psql_completion(const char *text, int start, int end) else if ((HeadMatches1("GRANT") && TailMatches1("TO")) || (HeadMatches1("REVOKE") && TailMatches1("FROM"))) COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); - + else if (HeadMatches3("ALTER","DEFAULT", "PRIVILEGES") && TailMatches1("TO|FROM")) + COMPLETE_WITH_QUERY(Query_for_list_of_grant_roles); /* Complete "GRANT/REVOKE ... ON * *" with TO/FROM */ else if (HeadMatches1("GRANT") && TailMatches3("ON", MatchAny, MatchAny)) COMPLETE_WITH_CONST("TO"); --------------44BB8EA2DDE37F779CC6A6EA Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --------------44BB8EA2DDE37F779CC6A6EA--