Hi 2017-05-07 22:55 GMT+02:00 Fabien COELHO <coe...@cri.ensmp.fr>:
> > Hello Pavel, > > Sometimes I have to solve the result types of some query. It is invisible >>> in psql. >>> >> > Indeed. My 0.02€ about this patch: > > > About the feature: > > It looks useful to allow to show the resulting types of a query. > > > About the code: > > Patch applies cleanly and compiles. > > I'm afraid that re-using the "results" variable multiple times results in > memory leaks... ISTM that new variables should be used when going down the > nested conditions, and all cleanup should be done where and when necessary. > > Also, maybe it would be better if the statement is cleaned up server side > at the end of the execution. Not sure how to achieve that, though, libpq > seems to lack the relevant function:-( > > """although there is no libpq function for deleting a prepared > statement, the SQL DEALLOCATE statement can be used for that purpose.""" > > Hmmm... I have not found how to use DEALLOCATE to cleanup an unnamed > statement, it does not allow a "zero-length" name. Maybe it could be > extended somehow, or a function could be provided for the purpose, eg > by passing a NULL query to PQprepare... > > Resetting "gdesc flag" could be done only when the flag was true, at the > end of the if, rather than on each query. > I don't think - now it is processed in sendquery_cleanup and it is consistent with other flags. "if" is used only when some memory releasing is necessary. > I understand that the second level query is to retrieve the type names in > place of the Oid returned by QPftype? > yes, DESCRIBE doesn't return text type names. > > The pg_malloc length computation looks a little bit arbitrary. Would it > make sense to use PQescapeLiteral instead? > done > > > About the documentation: > > I would suggest some rewording, maybe: > > "Show the description of the result of the current query buffer without > actually executing it, by considering it a prepared statement." > > > done > About tests: > > There should be some non-regression tests added, maybe something like: > > SELECT > NULL AS zero, > 1 AS one, > 2.0 AS two, > 'three' AS three, > $1 AS four, > CURRENT_DATE AS now > -- ... > \gdesc > > And also case which trigger an error, eg: > done > > SELECT $1 AS unknown_type \gdesc > It is not unknown type - the default placeholder type is text > SELECT 1 + \gdesc > > Some fun: > > PREPARE test AS SELECT 1; > EXECUTE test \gdesc > ... > > I'm unsure about some error messages, but it may be unrelated to this > patch: > > calvin=# SELECT '1 km'::unit AS foo, $2 as boo \gdesc > ERROR: could not determine data type of parameter $1 Looks like messy PostgreSQL error message. You miss "$1" placeholder attached updated patch Regards Pavel > > -- > Fabien.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 3b86612..2e46f4c 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> + Show the description of the result of the current query buffer without + actually executing it, by considering it a 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 b3263a9..a1c8e1d 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..a6d4756 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1323,7 +1323,80 @@ SendQuery(const char *query) } } - if (pset.fetch_count <= 0 || pset.gexec_flag || + if (pset.gdesc_flag) + { + /* + * Unnamed prepared statement is used. Is not possible to + * create any unnamed prepared statement from psql user space, + * so there should not be any conflict. In this moment is not + * possible to deallocate this prepared statement, so it should + * to live to end of session or to another \gdesc call. + */ + results = PQprepare(pset.db, "", query, 0, NULL); + if (PQresultStatus(results) != PGRES_COMMAND_OK) + { + psql_error("%s", PQerrorMessage(pset.db)); + ClearOrSaveResult(results); + ResetCancelConn(); + goto sendquery_cleanup; + } + PQclear(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 = PQescapeLiteral(pset.db, name, name_length); + + if (escname == NULL) + { + psql_error("%s", PQerrorMessage(pset.db)); + PQclear(results); + termPQExpBuffer(&buf); + goto sendquery_cleanup; + } + + appendPQExpBuffer(&buf, "(%s, %d, %d)", + escname, PQftype(results,i), PQfmod(results,i)); + PQfreemem(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); + } + + ClearOrSaveResult(results); + 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 +1551,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 183fc37..e6a0b4a 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", diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out index d602aee..5e4b448 100644 --- a/src/test/regress/expected/psql.out +++ b/src/test/regress/expected/psql.out @@ -2964,3 +2964,36 @@ SELECT 3 UNION SELECT 4 UNION SELECT 5 ORDER BY 1; +-- gdesc tests +SELECT + NULL AS zero, + 1 AS one, + 2.0 AS two, + 'three' AS three, + $1 AS four, + sin($2) as five, + CURRENT_DATE AS now +\gdesc + Name | Type +-------+------------------ + zero | text + one | integer + two | numeric + three | text + four | text + five | double precision + now | date +(7 rows) + +PREPARE test AS SELECT 1 AS first; +EXECUTE test \gdesc + Name | Type +-------+--------- + first | integer +(1 row) + +-- should fail - syntax error +SELECT 1 + \gdesc +ERROR: syntax error at end of input +LINE 1: SELECT 1 + + ^ diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql index b56a05f..37235ff 100644 --- a/src/test/regress/sql/psql.sql +++ b/src/test/regress/sql/psql.sql @@ -560,3 +560,20 @@ UNION SELECT 5 ORDER BY 1; \r \p + +-- gdesc tests +SELECT + NULL AS zero, + 1 AS one, + 2.0 AS two, + 'three' AS three, + $1 AS four, + sin($2) as five, + CURRENT_DATE AS now +\gdesc + +PREPARE test AS SELECT 1 AS first; +EXECUTE test \gdesc + +-- should fail - syntax error +SELECT 1 + \gdesc
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers