Re: Tab completion for CREATE SCHEMAAUTHORIZATION - Mailing list pgsql-hackers

From ilmari@ilmari.org (Dagfinn Ilmari Mannsåker)
Subject Re: Tab completion for CREATE SCHEMAAUTHORIZATION
Date
Msg-id 874kc1gdxc.fsf@wibble.ilmari.org
Whole thread Raw
In response to Re: Tab completion for CREATE SCHEMAAUTHORIZATION  (ilmari@ilmari.org (Dagfinn Ilmari Mannsåker))
Responses Re: Tab completion for CREATE SCHEMAAUTHORIZATION  (Suraj Khamkar <khamkarsuraj.b@gmail.com>)
List pgsql-hackers
ilmari@ilmari.org (Dagfinn Ilmari Mannsåker) writes:

> ilmari@ilmari.org (Dagfinn Ilmari Mannsåker) writes:
>
>> Hi Hackers,
>>
>> I just noticed there's no tab completion for CREATE SCHEMA
>> AUTHORIZATION, nor for anything after CREATE SCHEMA <name>.
>>
>> Please find attached a patch that adds this.
>
> Added to the 2021-09 commit fest: https://commitfest.postgresql.org/34/3252/

Here's an updated version that also reduces the duplication between the
various role list queries.

- ilmari

From 52c1fe9900543253510b64e58de24f920d8b0dc5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= <ilmari@ilmari.org>
Date: Fri, 9 Jul 2021 17:15:00 +0100
Subject: [PATCH v2] Add tab completion for CREATE SCHEMA

In passing, reduce duplication between the various role list queries
---
 src/bin/psql/tab-complete.c | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 064892bade..adddd45f59 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -758,15 +758,16 @@ static const SchemaQuery Query_for_list_of_collations = {
 "   FROM pg_catalog.pg_roles "\
 "  WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"
 
-#define Query_for_list_of_grant_roles \
-" SELECT pg_catalog.quote_ident(rolname) "\
-"   FROM pg_catalog.pg_roles "\
-"  WHERE substring(pg_catalog.quote_ident(rolname),1,%d)='%s'"\
-" UNION ALL SELECT 'PUBLIC'"\
-" UNION ALL SELECT 'CURRENT_ROLE'"\
+#define Query_for_list_of_schema_roles \
+Query_for_list_of_roles \
 " UNION ALL SELECT 'CURRENT_USER'"\
 " UNION ALL SELECT 'SESSION_USER'"
 
+#define Query_for_list_of_grant_roles \
+Query_for_list_of_schema_roles \
+" UNION ALL SELECT 'PUBLIC'"\
+" UNION ALL SELECT 'CURRENT_ROLE'"
+
 /* the silly-looking length condition is just to eat up the current word */
 #define Query_for_index_of_table \
 "SELECT pg_catalog.quote_ident(c2.relname) "\
@@ -2675,6 +2676,19 @@ psql_completion(const char *text, int start, int end)
     else if (TailMatches("AS", "ON", "SELECT|UPDATE|INSERT|DELETE", "TO"))
         COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
 
+/* CREATE SCHEMA [ <name> ] [ AUTHORIZATION ] */
+    else if (Matches("CREATE", "SCHEMA"))
+        COMPLETE_WITH_QUERY(Query_for_list_of_schemas
+                            " UNION ALL SELECT 'AUTHORIZATION'");
+    else if (Matches("CREATE", "SCHEMA", "AUTHORIZATION"))
+        COMPLETE_WITH_QUERY(Query_for_list_of_schema_roles);
+    else if (Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION"))
+        COMPLETE_WITH_QUERY(Query_for_list_of_schema_roles);
+    else if (Matches("CREATE", "SCHEMA", MatchAny, "AUTHORIZATION", MatchAny))
+        COMPLETE_WITH("CREATE", "GRANT");
+    else if (Matches("CREATE", "SCHEMA", MatchAny))
+        COMPLETE_WITH("AUTHORIZATION", "CREATE", "GRANT");
+
 /* CREATE SEQUENCE --- is allowed inside CREATE SCHEMA, so use TailMatches */
     else if (TailMatches("CREATE", "SEQUENCE", MatchAny) ||
              TailMatches("CREATE", "TEMP|TEMPORARY", "SEQUENCE", MatchAny))
-- 
2.30.2


pgsql-hackers by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Commitfest overflow
Next
From: "David G. Johnston"
Date:
Subject: Re: Commitfest overflow