Thread: Fix for psql \d tab completion

Fix for psql \d tab completion

From
Bruce Momjian
Date:
Robert Treat reported to me that \dS tab completion was not working in
CVS HEAD.  I have adjusted the code to handle this and be more flexible
about \d modifiers.

--
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: src/bin/psql/tab-complete.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v
retrieving revision 1.181
diff -c -c -r1.181 tab-complete.c
*** src/bin/psql/tab-complete.c    27 Mar 2009 14:58:46 -0000    1.181
--- src/bin/psql/tab-complete.c    6 Apr 2009 15:39:45 -0000
***************
*** 431,448 ****
  "  UNION ALL SELECT 'all') ss "\
  " WHERE substring(name,1,%d)='%s'"

- /*
-  * Note: As of Pg 8.2, we no longer use relkind 's', but we keep it here
-  * for compatibility with older servers
-  */
- #define Query_for_list_of_system_relations \
- "SELECT pg_catalog.quote_ident(relname) "\
- "  FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "\
- " WHERE c.relkind IN ('r', 'v', 's', 'S') "\
- "   AND substring(pg_catalog.quote_ident(relname),1,%d)='%s' "\
- "   AND c.relnamespace = n.oid "\
- "   AND n.nspname = 'pg_catalog'"
-
  #define Query_for_list_of_roles \
  " SELECT pg_catalog.quote_ident(rolname) "\
  "   FROM pg_catalog.pg_roles "\
--- 431,436 ----
***************
*** 2183,2230 ****
  /* TODO:  \dc \dd \dl */
      else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_databases);
!     else if (strcmp(prev_wd, "\\d") == 0 || strcmp(prev_wd, "\\d+") == 0)
!         COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tisv, NULL);
!     else if (strcmp(prev_wd, "\\da") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);
!     else if (strcmp(prev_wd, "\\db") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
!     else if (strcmp(prev_wd, "\\dD") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
!     else if (strcmp(prev_wd, "\\des") == 0 || strcmp(prev_wd, "\\des+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_servers);
!     else if (strcmp(prev_wd, "\\deu") == 0 || strcmp(prev_wd, "\\deu+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
!     else if (strcmp(prev_wd, "\\dew") == 0 || strcmp(prev_wd, "\\dew+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
!     else if (strcmp(prev_wd, "\\df") == 0 || strcmp(prev_wd, "\\df+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
!     else if (strcmp(prev_wd, "\\dF") == 0 || strcmp(prev_wd, "\\dF+") == 0)
!         COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations);
!     else if (strcmp(prev_wd, "\\dFd") == 0 || strcmp(prev_wd, "\\dFd+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries);
!     else if (strcmp(prev_wd, "\\dFp") == 0 || strcmp(prev_wd, "\\dFp+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers);
!     else if (strcmp(prev_wd, "\\dFt") == 0 || strcmp(prev_wd, "\\dFt+") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates);
!     else if (strcmp(prev_wd, "\\di") == 0 || strcmp(prev_wd, "\\di+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
!     else if (strcmp(prev_wd, "\\dn") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
!     else if (strcmp(prev_wd, "\\dp") == 0 || strcmp(prev_wd, "\\z") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsv, NULL);
!     else if (strcmp(prev_wd, "\\ds") == 0 || strcmp(prev_wd, "\\ds+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
!     else if (strcmp(prev_wd, "\\dS") == 0 || strcmp(prev_wd, "\\dS+") == 0)
!         COMPLETE_WITH_QUERY(Query_for_list_of_system_relations);
!     else if (strcmp(prev_wd, "\\dt") == 0 || strcmp(prev_wd, "\\dt+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
!     else if (strcmp(prev_wd, "\\dT") == 0 || strcmp(prev_wd, "\\dT+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
!     else if (strcmp(prev_wd, "\\du") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_roles);
!     else if (strcmp(prev_wd, "\\dv") == 0 || strcmp(prev_wd, "\\dv+") == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
      else if (strcmp(prev_wd, "\\encoding") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_encodings);
      else if (strcmp(prev_wd, "\\h") == 0 || strcmp(prev_wd, "\\help") == 0)
--- 2171,2223 ----
  /* TODO:  \dc \dd \dl */
      else if (strcmp(prev_wd, "\\connect") == 0 || strcmp(prev_wd, "\\c") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_databases);
!
!     else if (strncmp(prev_wd, "\\da", strlen("\\da")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_aggregates, NULL);
!     else if (strncmp(prev_wd, "\\db", strlen("\\db")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_tablespaces);
!     else if (strncmp(prev_wd, "\\dD", strlen("\\dD")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_domains, NULL);
!     else if (strncmp(prev_wd, "\\des", strlen("\\des")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_servers);
!     else if (strncmp(prev_wd, "\\deu", strlen("\\deu")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_user_mappings);
!     else if (strncmp(prev_wd, "\\dew", strlen("\\dew")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_fdws);
!
!     else if (strncmp(prev_wd, "\\df", strlen("\\df")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_functions, NULL);
!     else if (strncmp(prev_wd, "\\dFd", strlen("\\dFd")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_dictionaries);
!     else if (strncmp(prev_wd, "\\dFp", strlen("\\dFp")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_parsers);
!     else if (strncmp(prev_wd, "\\dFt", strlen("\\dFt")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_ts_templates);
!     /* must be at end of \dF */
!     else if (strncmp(prev_wd, "\\dF", strlen("\\dF")) == 0)
!         COMPLETE_WITH_QUERY(Query_for_list_of_ts_configurations);
!
!     else if (strncmp(prev_wd, "\\di", strlen("\\di")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_indexes, NULL);
!     else if (strncmp(prev_wd, "\\dn", strlen("\\dn")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_schemas);
!     else if (strncmp(prev_wd, "\\dp", strlen("\\dp")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tsv, NULL);
!     else if (strncmp(prev_wd, "\\ds", strlen("\\ds")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_sequences, NULL);
!     else if (strncmp(prev_wd, "\\dt", strlen("\\dt")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tables, NULL);
!     else if (strncmp(prev_wd, "\\dT", strlen("\\dT")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_datatypes, NULL);
!     else if (strncmp(prev_wd, "\\du", strlen("\\du")) == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_roles);
!     else if (strncmp(prev_wd, "\\dv", strlen("\\dv")) == 0)
          COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_views, NULL);
+
+     /* must be at end of \d list */
+     else if (strncmp(prev_wd, "\\d", strlen("\\d")) == 0)
+         COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_tisv, NULL);
+
      else if (strcmp(prev_wd, "\\encoding") == 0)
          COMPLETE_WITH_QUERY(Query_for_list_of_encodings);
      else if (strcmp(prev_wd, "\\h") == 0 || strcmp(prev_wd, "\\help") == 0)

Re: Fix for psql \d tab completion

From
Alvaro Herrera
Date:
Bruce Momjian wrote:
> Robert Treat reported to me that \dS tab completion was not working in
> CVS HEAD.  I have adjusted the code to handle this and be more flexible
> about \d modifiers.

Doesn't this break completion for stuff like \daS, etc?



-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


Re: Fix for psql \d tab completion

From
Tom Lane
Date:
Alvaro Herrera <alvherre@commandprompt.com> writes:
> Bruce Momjian wrote:
>> Robert Treat reported to me that \dS tab completion was not working in
>> CVS HEAD.  I have adjusted the code to handle this and be more flexible
>> about \d modifiers.

> Doesn't this break completion for stuff like \daS, etc?

AFAICS \daS still works (that's why it's strncmp not just strcmp).

The thing that still doesn't work particularly well is completion
on the \dtisv family; this coding will only complete for the type
of object denoted by the first letter after \d.  It's not clear how
important that is in the real world, though; and it wasn't any better
before.
        regards, tom lane