2015-07-02 11:03 GMT+02:00 Heikki Linnakangas <hlinn...@iki.fi>: > On 05/29/2015 10:41 AM, Pavel Stehule wrote: > >> 2015-05-29 9:28 GMT+02:00 Jeevan Chalke <jeevan.cha...@gmail.com>: >> >> I agree with Peter that "We don't tab-complete everything we possibly >>> could", but using tabs after "SET ROLE TO " provides "DEFAULT" as an >>> option >>> which seems wrong. >>> This patch adds list of roles over there, which I guess good to have than >>> giving something unusual. >>> >>> ... >> >> But back to this topic. I am thinking so it is little bit different due >> fact so we support two very syntax for one feature. And looks little bit >> strange, so one way is supported by autocomplete and second not. >> > > Yeah, it's a bit strange. We have a specific autocomplete rule for "SET > ROLE", but "SET ROLE TO" is treated as a generic GUC. With your patch, we'd > also lose the auto-completion to "SET ROLE TO DEFAULT". > > I think we want to encourage people to use the SQL-standard syntax "SET > ROLE ..." rather than the PostgreSQL-specific "SET ROLE TO ...". On the > whole, this just doesn't seem like much of an improvement. I'll mark this > as 'rejected' in the commitfest app. > > PS. I note that the auto-completion for "SET XXX TO ... is pretty lousy in > general. We have rules for DateStyle, IntervalStyle, GEQO and search_path, > but that's it. That could be expanded a lot. All enum-type GUCs could be > handled with a single rule that queries pg_settings.enumvals, for example, > and booleans would be easy too. But that's a different story. >
I wrote a patch for fallback tabcomplete for bool and enum GUC variables Regards Pavel > > - Heikki > >
commit 7749d7d3fabf468dbe2c5f397add9f8e31f59614 Author: Pavel Stehule <pavel.steh...@gooddata.com> Date: Wed Jul 8 14:24:55 2015 +0200 initial diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 0683548..c4e56c8 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -742,6 +742,14 @@ static const SchemaQuery Query_for_list_of_matviews = { " FROM pg_catalog.pg_tablesample_method "\ " WHERE substring(pg_catalog.quote_ident(tsmname),1,%d)='%s'" +#define Query_for_enum \ +" SELECT name FROM ( "\ +" SELECT unnest(enumvals) AS name "\ +" FROM pg_catalog.pg_settings "\ +" WHERE pg_catalog.lower(name)=pg_catalog.lower('%s') "\ +" UNION SELECT 'DEFAULT' ) ss "\ +" WHERE pg_catalog.substring(name,1,%%d)='%%s'" + /* * This is a list of all "things" in Pgsql, which can show up after CREATE or * DROP; and there is also a query to get a list of them. @@ -832,6 +840,8 @@ static PGresult *exec_query(const char *query); static void get_previous_words(int point, char **previous_words, int nwords); +static char *get_vartype(const char *varname); + #ifdef NOT_USED static char *quote_file_name(char *text, int match_type, char *quote_pointer); static char *dequote_file_name(char *text, char quote_char); @@ -3548,20 +3558,6 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH_LIST(my_list); } - else if (pg_strcasecmp(prev2_wd, "IntervalStyle") == 0) - { - static const char *const my_list[] = - {"postgres", "postgres_verbose", "sql_standard", "iso_8601", NULL}; - - COMPLETE_WITH_LIST(my_list); - } - else if (pg_strcasecmp(prev2_wd, "GEQO") == 0) - { - static const char *const my_list[] = - {"ON", "OFF", "DEFAULT", NULL}; - - COMPLETE_WITH_LIST(my_list); - } else if (pg_strcasecmp(prev2_wd, "search_path") == 0) { COMPLETE_WITH_QUERY(Query_for_list_of_schemas @@ -3571,10 +3567,31 @@ psql_completion(const char *text, int start, int end) } else { - static const char *const my_list[] = - {"DEFAULT", NULL}; + /* fallback for GUC settings */ - COMPLETE_WITH_LIST(my_list); + char *vartype = get_vartype(prev2_wd); + + if (strcmp(vartype, "enum") == 0) + { + char querybuf[1024]; + + snprintf(querybuf, 1024, Query_for_enum, prev2_wd); + COMPLETE_WITH_QUERY(querybuf); + } + else if (strcmp(vartype, "bool") == 0) + { + static const char *const my_list[] = + {"ON", "OFF", "DEFAULT", NULL}; + + COMPLETE_WITH_LIST(my_list); + } + else + { + static const char *const my_list[] = + {"DEFAULT", NULL}; + + COMPLETE_WITH_LIST(my_list); + } } } @@ -4636,6 +4653,42 @@ get_previous_words(int point, char **previous_words, int nwords) } } +static char * +get_vartype(const char *varname) +{ + PQExpBufferData query_buffer; + char *e_varname; + PGresult *result; + int string_length; + static char resbuf[10]; + + initPQExpBuffer(&query_buffer); + + string_length = strlen(varname); + e_varname = pg_malloc(string_length * 2 + 1); + PQescapeString(e_varname, varname, string_length); + + appendPQExpBuffer(&query_buffer, + "SELECT vartype FROM pg_settings WHERE pg_catalog.lower(name) = pg_catalog.lower('%s')", + e_varname); + + result = exec_query(query_buffer.data); + termPQExpBuffer(&query_buffer); + free(e_varname); + + resbuf[0] = '\0'; + + if (PQresultStatus(result) == PGRES_TUPLES_OK) + { + if (PQntuples(result) > 0) + strncpy(resbuf, PQgetvalue(result, 0, 0), 10); + } + + PQclear(result); + + return resbuf; +} + #ifdef NOT_USED /*
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers