Hello, I found that a typo(?) in tab-complete.c.

> /* ALTER TABLE,INDEX,MATERIALIZED VIEW xxx ALL IN TABLESPACE xxx OWNED BY */
> else if (pg_strcasecmp(prev6_wd, "ALL") == 0 &&
>                pg_strcasecmp(prev5_wd, "IN") == 0 &&
>                pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
>                pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
>                pg_strcasecmp(prev4_wd, "BY") == 0)

"BY" is compared against the word in wrong position and it
prevents this completion from matching.

I also found some other bugs in psql-completion. The attached
patch applied on master and fixes them all togher.

- Fix completion for ALL IN TABLESPACE OWNED BY.

- Fix the assumed syntax of CREATE INDEX where the CONCURRENTLY
  is misplaced. (It is assuming the order "CREATE INDEX sth
  CONCURRENTLY")

- Provide missing terminating NULL to the preposition list for
  SECURITY LABEL.

- Add the preposition list with some missing items. However, this
  would not be a kind of bug in constrast to the three items
  above, though. This applied back to 9.3 and 9.2 doesn't have
  "EVENT TRIGGER" and "MATERIALIZED VIEW" and 9.1 additionally
  doesn't have "DATABASE" and "ROLE". I'll provide individual
  patches if necessary.

regards,

-- 
Kyotaro Horiguchi
NTT Open Source Software Center
>From 36096e1afdda3f54800c2c73fe4f058a3eef25ef Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyot...@lab.ntt.co.jp>
Date: Mon, 2 Nov 2015 14:10:53 +0900
Subject: [PATCH] Fix some tab-completino bugs.

  - 'ALTER [TABLE|xx] xxx ALL IN TABLE SPACE xx OWNED BY' cannot be completed.
  - CONCURRENTLY in CREATE INDEX is misplaced.
  - Preposition list of SECURITY LABEL is not terminated.
  - The target list of SECURITY LABEL ON misses some alternatives.
---
 src/bin/psql/tab-complete.c | 54 ++++++++++++++++++++++-----------------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index 0e8d395..912811d 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -1040,7 +1040,7 @@ psql_completion(const char *text, int start, int end)
 			 pg_strcasecmp(prev5_wd, "IN") == 0 &&
 			 pg_strcasecmp(prev4_wd, "TABLESPACE") == 0 &&
 			 pg_strcasecmp(prev2_wd, "OWNED") == 0 &&
-			 pg_strcasecmp(prev4_wd, "BY") == 0)
+			 pg_strcasecmp(prev_wd, "BY") == 0)
 	{
 		COMPLETE_WITH_QUERY(Query_for_list_of_roles);
 	}
@@ -2320,35 +2320,34 @@ psql_completion(const char *text, int start, int end)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes,
 								   " UNION SELECT 'ON'"
 								   " UNION SELECT 'CONCURRENTLY'");
+	/* If we have INDEX CONCURRENTLY <sth>, then add exiting indexes */
+	else if (pg_strcasecmp(prev2_wd, "INDEX") == 0 &&
+			 pg_strcasecmp(prev_wd, "CONCURRENTLY") == 0)
+			 COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
 	/* Complete ... INDEX [<name>] ON with a list of tables  */
 	else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev2_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev2_wd, "CONCURRENTLY") == 0) &&
+			  (pg_strcasecmp(prev4_wd, "INDEX") == 0 &&
+			   pg_strcasecmp(prev3_wd, "CONCURRENTLY") == 0)) &&
 			 pg_strcasecmp(prev_wd, "ON") == 0)
 		COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tm, NULL);
-	/* If we have CREATE|UNIQUE INDEX <sth> CONCURRENTLY, then add "ON" */
-	else if ((pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev2_wd, "INDEX") == 0) &&
-			 pg_strcasecmp(prev_wd, "CONCURRENTLY") == 0)
+	/* If we have CREATE|UNIQUE INDEX [CONCURRENTLY] <sth>, then add "ON" */
+	else if (((pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
+			   pg_strcasecmp(prev3_wd, "UNIQUE") == 0) &&
+			  pg_strcasecmp(prev2_wd, "INDEX") == 0 &&
+			  pg_strcasecmp(prev_wd, "CONCURRENTLY") != 0) ||
+			 ((pg_strcasecmp(prev4_wd, "CREATE") == 0 ||
+			   pg_strcasecmp(prev4_wd, "UNIQUE") == 0) &&
+			  pg_strcasecmp(prev3_wd, "INDEX") == 0 &&
+			  pg_strcasecmp(prev2_wd, "CONCURRENTLY") == 0))
 		COMPLETE_WITH_CONST("ON");
-	/* If we have CREATE|UNIQUE INDEX <sth>, then add "ON" or "CONCURRENTLY" */
-	else if ((pg_strcasecmp(prev3_wd, "CREATE") == 0 ||
-			  pg_strcasecmp(prev3_wd, "UNIQUE") == 0) &&
-			 pg_strcasecmp(prev2_wd, "INDEX") == 0)
-	{
-		static const char *const list_CREATE_INDEX[] =
-		{"CONCURRENTLY", "ON", NULL};
-
-		COMPLETE_WITH_LIST(list_CREATE_INDEX);
-	}
 
 	/*
-	 * Complete INDEX <name> ON <table> with a list of table columns (which
-	 * should really be in parens)
+	 * Complete INDEX [CONCURRENTLY] <name> ON <table> with a list of table
+	 * columns (which should really be in parens)
 	 */
 	else if ((pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev3_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev3_wd, "CONCURRENTLY") == 0) &&
+			  (pg_strcasecmp(prev5_wd, "INDEX") == 0 &&
+			   pg_strcasecmp(prev4_wd, "CONCURRENTLY") == 0)) &&
 			 pg_strcasecmp(prev2_wd, "ON") == 0)
 	{
 		static const char *const list_CREATE_INDEX2[] =
@@ -2357,8 +2356,8 @@ psql_completion(const char *text, int start, int end)
 		COMPLETE_WITH_LIST(list_CREATE_INDEX2);
 	}
 	else if ((pg_strcasecmp(prev5_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev4_wd, "INDEX") == 0 ||
-			  pg_strcasecmp(prev4_wd, "CONCURRENTLY") == 0) &&
+			  (pg_strcasecmp(prev6_wd, "INDEX") == 0 &&
+			   pg_strcasecmp(prev5_wd, "CONCURRENTLY") == 0)) &&
 			 pg_strcasecmp(prev3_wd, "ON") == 0 &&
 			 pg_strcasecmp(prev_wd, "(") == 0)
 		COMPLETE_WITH_ATTR(prev2_wd, "");
@@ -3555,7 +3554,7 @@ psql_completion(const char *text, int start, int end)
 			 pg_strcasecmp(prev_wd, "LABEL") == 0)
 	{
 		static const char *const list_SECURITY_LABEL_preposition[] =
-		{"ON", "FOR"};
+			{"ON", "FOR", NULL};
 
 		COMPLETE_WITH_LIST(list_SECURITY_LABEL_preposition);
 	}
@@ -3572,9 +3571,10 @@ psql_completion(const char *text, int start, int end)
 			  pg_strcasecmp(prev_wd, "ON") == 0))
 	{
 		static const char *const list_SECURITY_LABEL[] =
-		{"LANGUAGE", "SCHEMA", "SEQUENCE", "TABLE", "TYPE", "VIEW",
-			"MATERIALIZED VIEW", "COLUMN", "AGGREGATE", "FUNCTION", "DOMAIN",
-		"LARGE OBJECT", NULL};
+			{"TABLE", "COLUMN", "AGGREGATE", "DATABASE", "DOMAIN",
+			 "EVENT TRIGGER", "FOREIGN TABLE", "FUNCTION", "LARGE OBJECT",
+			 "MATERIALIZED VIEW", "LANGUAGE", "ROLE", "SCHEMA", 
+			 "SEQUENCE", "TYPE", "VIEW", NULL};
 
 		COMPLETE_WITH_LIST(list_SECURITY_LABEL);
 	}
-- 
1.8.3.1

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to