Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mJZTX-0003ea-HF for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Aug 2021 10:52:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mJZTV-0003gd-4s for pgsql-hackers@arkaria.postgresql.org; Fri, 27 Aug 2021 10:52:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mJZTU-0003fg-S4 for pgsql-hackers@lists.postgresql.org; Fri, 27 Aug 2021 10:52:40 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mJZTS-0008O0-RU for pgsql-hackers@lists.postgresql.org; Fri, 27 Aug 2021 10:52:40 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 59C0B320093E; Fri, 27 Aug 2021 06:52:36 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Fri, 27 Aug 2021 06:52:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h= from:to:cc:subject:references:date:in-reply-to:message-id :mime-version:content-type; s=fm2; bh=wP1pgailCwRiMEyfBbTOiB/Mqe r2+t1pvlN4Ex0x5bo=; b=qgJm22r51B9SF0kZslfSjpglFtVuxZT4OOnj1KDWNO bQdyseJZEh3d26EJbNG+51qKhF7zbrWSrDFSm0BV++dfuXoQTiw0klJu0LCksh80 JYny9K+fY4lWahSYSKcTD65idUT0PjKvfDud3gjj+IuatC4wqKhBcGNhiKuM9mKa XbiyOkCQRGAxBoWKDmE5IEMWblF5x7/m46yBk5vFTtvMOb6PFKyTQlnblj30efX3 8gtDj6QJ3XXSRUX5qkTuFKtD9DDV7fmkZpp8VJ/BE38ILiIKi/a0rhbbKWt56wRj 41ZM2HnjpWXCwrEeW16CCDcJHuj9rZRQFq2+HqfvQQHw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=wP1pga ilCwRiMEyfBbTOiB/Mqer2+t1pvlN4Ex0x5bo=; b=jzsQhCug6Bjt+y2vfFP9CF sXKB41pSI99o2ivrx65VSQ1rQcPwBITc4B8MP/RL9SgrfA4mAkcu8HkMBg91SCAs XWM5APVKjU4icnru309W0cVeDeHPR4/1tHpMp+2nDyANKVRIgFON1aRilNs4FTzw jihxDLEE2vuHENsZ3IhlRKeR/SKSMkep0duJK8mew9App43l36ncnjrCGbfxG1Lg 2GxQZzDpZCvYTfq3X2OXkQ/eytRZJd9YQ2Oi4UfgYotsqorQi5jDACkq+ft2m7pT DzHqolWxswFhj+T0YOGCQcq9TO4r/rNFVqWEz0tupfhYf/7To8RvvgHvDgglDHgQ == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddruddufedgfeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfeehmdenucfjughrpefhvffufhffjgfkfgggtgesmhdtreertder jeenucfhrhhomhepffgrghhfihhnnhgpkfhlmhgrrhhipgforghnnhhsnohkvghruceoih hlmhgrrhhisehilhhmrghrihdrohhrgheqnecuggftrfgrthhtvghrnhepteevieehteet leeglefghfefkeejheelledttedtgfdvkeffheffvdekheeuudffnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhlmhgrrhhisehilhhmrghr ihdrohhrgh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 27 Aug 2021 06:52:34 -0400 (EDT) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: Michael Paquier Cc: pgsql-hackers@lists.postgresql.org Subject: Re: [PATCH] Tab completion for ALTER TABLE =?utf-8?Q?=E2=80=A6?= ADD =?utf-8?Q?=E2=80=A6?= References: <87bl6ehhpl.fsf@wibble.ilmari.org> Date: Fri, 27 Aug 2021 11:52:33 +0100 In-Reply-To: (Michael Paquier's message of "Fri, 27 Aug 2021 13:04:00 +0900") Message-ID: <87k0k7upla.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Michael Paquier writes: > On Tue, Aug 03, 2021 at 12:48:38PM +0100, Dagfinn Ilmari Mannsåker wrote: >> The other day I noticed that there's no tab completion after ALTER TABLE >> … ADD, so here's a patch. In addition to COLUMN and all the table >> constraint types, it also completes with the list of unique indexes on >> the table after ALTER TABLE … ADD … USING INDEX. > > I was reading this patch (not actually tested), and that's a clear > improvement. One extra thing that could be done here is to complete > with types for a ALTER TABLE ADD COLUMN foo. That was easy enough to add (just a bit of extra fiddling to handle COLUMN being optional), done in the attached v2 patch. > We could as well have a list of columns after UNIQUE or PRIMARY KEY, > but that feels like extra cream on top of the cake. Doing a list of arbitrarily many comma-separated names is more complicated, so that can be the subject for another patch. > In short I am fine with what you have here. Thanks for the review. - ilmari --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=v2-0001-Add-tab-completion-for-ALTER-TABLE-ADD.patch From 0bdf91f2a66bf9393e6900db904bda1961c03350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Tue, 3 Aug 2021 12:23:07 +0100 Subject: [PATCH v2] =?UTF-8?q?Add=20tab=20completion=20for=20ALTER=20TABLE?= =?UTF-8?q?=20=E2=80=A6=20ADD=20=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Complete with COLUMN plus the various table constraint types, list unique indexes on the table after ADD (UNQIUE|PRIMARY KEY) USING INDEX, and data types after ADD [COLUMN] . --- src/bin/psql/tab-complete.c | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 7cdfc7c637..bb7c3fc5cf 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -776,6 +776,10 @@ static const SchemaQuery Query_for_list_of_collations = { " and pg_catalog.quote_ident(c1.relname)='%s'"\ " and pg_catalog.pg_table_is_visible(c2.oid)" +#define Query_for_unique_index_of_table \ +Query_for_index_of_table \ +" and i.indisunique" + /* the silly-looking length condition is just to eat up the current word */ #define Query_for_constraint_of_table \ "SELECT pg_catalog.quote_ident(conname) "\ @@ -2019,6 +2023,51 @@ psql_completion(const char *text, int start, int end) "OWNER TO", "SET", "VALIDATE CONSTRAINT", "REPLICA IDENTITY", "ATTACH PARTITION", "DETACH PARTITION", "FORCE ROW LEVEL SECURITY"); + /* ALTER TABLE xxx ADD */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD")) + COMPLETE_WITH("COLUMN", "CONSTRAINT", "CHECK", "UNIQUE", "PRIMARY KEY", + "EXCLUDE", "FOREIGN KEY"); + /* ALTER TABLE xxx ADD CONSTRAINT yyy */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny)) + COMPLETE_WITH("CHECK", "UNIQUE", "PRIMARY KEY", "EXCLUDE", "FOREIGN KEY"); + /* ALTER TABLE xxx ADD (CONSTRAINT yyy)? FOREIGN|PRIMARY */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "FOREIGN|PRIMARY") || + Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "FOREIGN|PRIMARY")) + COMPLETE_WITH("KEY"); + /* ALTER TABLE xxx ADD (CONSTRAINT yyy)? (PRIMARY KEY|UNIQUE) */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY") || + Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE") || + Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "PRIMARY", "KEY") || + Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, "UNIQUE")) + COMPLETE_WITH("(", "USING INDEX"); + /* ALTER TABLE xxx ADD (CONSTRAINT yyy)? ... USING INDEX */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "PRIMARY", "KEY", "USING", "INDEX")) + { + completion_info_charp = prev6_wd; + COMPLETE_WITH_QUERY(Query_for_unique_index_of_table); + } + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "UNIQUE", "USING", "INDEX")) + { + completion_info_charp = prev5_wd; + COMPLETE_WITH_QUERY(Query_for_unique_index_of_table); + } + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, + "PRIMARY", "KEY", "USING", "INDEX")) + { + completion_info_charp = prev8_wd; + COMPLETE_WITH_QUERY(Query_for_unique_index_of_table); + } + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "CONSTRAINT", MatchAny, + "UNIQUE", "USING", "INDEX")) + { + completion_info_charp = prev7_wd; + COMPLETE_WITH_QUERY(Query_for_unique_index_of_table); + } + /* ADD column_name must be last of the ALTER TABLE xxx ADD matches */ + else if (Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN", MatchAny) || + (Matches("ALTER", "TABLE", MatchAny, "ADD", MatchAny) && + !Matches("ALTER", "TABLE", MatchAny, "ADD", "COLUMN"))) + COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL); /* ALTER TABLE xxx ENABLE */ else if (Matches("ALTER", "TABLE", MatchAny, "ENABLE")) COMPLETE_WITH("ALWAYS", "REPLICA", "ROW LEVEL SECURITY", "RULE", -- 2.30.2 --=-=-=--