2017-04-28 6:08 GMT+02:00 Pavel Stehule <pavel.steh...@gmail.com>: > Hi > > Sometimes I have to solve the result types of some query. It is invisible > in psql. You have to materialize table or you have to create view. Now, > when we can enhance \g command, we can introduce query describing > > some like > > select a, b from foo > \gdesc > > | type | length | collation | .... > ------------------------------------------------ > a | varchar | 30 | > b | numeric | 20 | > > here is the patch. It is based on PQdescribePrepared result.
postgres=# select * from pg_proc \gdesc ┌─────────────────┬──────────────┐ │ Name │ Type │ ╞═════════════════╪══════════════╡ │ proname │ name │ │ pronamespace │ oid │ │ proowner │ oid │ │ prolang │ oid │ │ procost │ real │ │ prorows │ real │ │ provariadic │ oid │ │ protransform │ regproc │ │ proisagg │ boolean │ │ proiswindow │ boolean │ │ prosecdef │ boolean │ │ proleakproof │ boolean │ │ proisstrict │ boolean │ │ proretset │ boolean │ │ provolatile │ "char" │ │ proparallel │ "char" │ │ pronargs │ smallint │ │ pronargdefaults │ smallint │ │ prorettype │ oid │ │ proargtypes │ oidvector │ │ proallargtypes │ oid[] │ │ proargmodes │ "char"[] │ │ proargnames │ text[] │ │ proargdefaults │ pg_node_tree │ │ protrftypes │ oid[] │ │ prosrc │ text │ │ probin │ text │ │ proconfig │ text[] │ │ proacl │ aclitem[] │ └─────────────────┴──────────────┘ (29 rows) > What do you think about this idea? > > Regards > > Pavel >
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 3b86612..03cba87 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1957,6 +1957,16 @@ Tue Oct 26 21:40:57 CEST 1999 <varlistentry> + <term><literal>\gdesc</literal></term> + <listitem> + <para> + Create unnamed prepared statement from current buffer and get and + show description of this prepared statement. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term><literal>\gexec</literal></term> <listitem> diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 859ded7..282e2e5 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -88,6 +88,7 @@ static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool ac static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd); +static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch); @@ -337,6 +338,8 @@ exec_command(const char *cmd, status = exec_command_f(scan_state, active_branch); else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0) status = exec_command_g(scan_state, active_branch, cmd); + else if (strcmp(cmd, "gdesc") == 0) + status = exec_command_gdesc(scan_state, active_branch); else if (strcmp(cmd, "gexec") == 0) status = exec_command_gexec(scan_state, active_branch); else if (strcmp(cmd, "gset") == 0) @@ -1328,6 +1331,25 @@ exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd) } /* + * \gdesc -- describe query result + */ +static backslashResult +exec_command_gdesc(PsqlScanState scan_state, bool active_branch) +{ + backslashResult status = PSQL_CMD_SKIP_LINE; + + if (active_branch) + { + pset.gdesc_flag = true; + status = PSQL_CMD_SEND; + } + else + ignore_slash_filepipe(scan_state); + + return status; +} + +/* * \gexec -- send query and execute each field of result */ static backslashResult diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index a2f1259..8a16744 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1323,7 +1323,62 @@ SendQuery(const char *query) } } - if (pset.fetch_count <= 0 || pset.gexec_flag || + if (pset.gdesc_flag) + { + results = PQprepare(pset.db, "", query, 0, NULL); + OK = ProcessResult(&results); + + if (OK && results) + { + results = PQdescribePrepared(pset.db, ""); + OK = ProcessResult(&results); + + if (OK && results) + { + PQExpBufferData buf; + int i; + + initPQExpBuffer(&buf); + printfPQExpBuffer(&buf, + "SELECT name AS \"Name\", pg_catalog.format_type(tp, tpm) AS \"Type\" FROM\n" + " (VALUES "); + + for(i = 0; i< PQnfields(results); i++) + { + char *name; + char *escname; + size_t name_length; + + if (i > 0) + appendPQExpBufferStr(&buf, ","); + + name = PQfname(results, i); + name_length = strlen(name); + escname = pg_malloc(name_length * 2 + 1); + PQescapeStringConn(pset.db, escname, name, name_length, NULL); + + appendPQExpBuffer(&buf, "('%s', %d, %d)", + escname, PQftype(results,i), PQfmod(results,i)); + free(escname); + } + + appendPQExpBuffer(&buf,") s (name, tp, tpm)"); + PQclear(results); + + results = PQexec(pset.db, buf.data); + OK = ProcessResult(&results); + + if (OK && results) + OK = PrintQueryResults(results); + + termPQExpBuffer(&buf); + } + } + + ResetCancelConn(); + results = NULL; /* PQclear(NULL) does nothing */ + } + else if (pset.fetch_count <= 0 || pset.gexec_flag || pset.crosstab_flag || !is_select_command(query)) { /* Default fetch-it-all-and-print mode */ @@ -1478,6 +1533,9 @@ sendquery_cleanup: pset.ctv_args[i] = NULL; } + /* reset \gdesc trigger */ + pset.gdesc_flag = false; + return OK; } diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index ac43522..6595df4 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -167,12 +167,13 @@ slashUsage(unsigned short int pager) * Use "psql --help=commands | wc" to count correctly. It's okay to count * the USE_READLINE line even in builds without that. */ - output = PageOutput(122, pager ? &(pset.popt.topt) : NULL); + output = PageOutput(123, pager ? &(pset.popt.topt) : NULL); fprintf(output, _("General\n")); fprintf(output, _(" \\copyright show PostgreSQL usage and distribution terms\n")); fprintf(output, _(" \\errverbose show most recent error message at maximum verbosity\n")); fprintf(output, _(" \\g [FILE] or ; execute query (and send results to file or |pipe)\n")); + fprintf(output, _(" \\gdesc describe result of query\n")); fprintf(output, _(" \\gx [FILE] as \\g, but forces expanded output mode\n")); fprintf(output, _(" \\gexec execute query, then execute each value in its result\n")); fprintf(output, _(" \\gset [PREFIX] execute query and store results in psql variables\n")); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 70ff181..a175912 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -95,6 +95,7 @@ typedef struct _psqlSettings char *gset_prefix; /* one-shot prefix argument for \gset */ bool gexec_flag; /* one-shot flag to execute query's results */ bool crosstab_flag; /* one-shot request to crosstab results */ + bool gdesc_flag; /* one-shot request to describe query */ char *ctv_args[4]; /* \crosstabview arguments */ bool notty; /* stdin or stdout is not a tty (as determined diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index e2a3351..cf591c5 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1410,7 +1410,7 @@ psql_completion(const char *text, int start, int end) "\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\drds", "\\ds", "\\dS", "\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy", "\\e", "\\echo", "\\ef", "\\encoding", "\\errverbose", "\\ev", - "\\f", "\\g", "\\gexec", "\\gset", "\\gx", "\\h", "\\help", "\\H", + "\\f", "\\g", "\\gdesc", "\\gexec", "\\gset", "\\gx", "\\h", "\\help", "\\H", "\\i", "\\ir", "\\l", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r", "\\s", "\\set", "\\setenv", "\\sf", "\\sv", "\\t",
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers