1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-02 09:02:37 +03:00

Allow psql's \df and \do commands to specify argument types.

When dealing with overloaded function or operator names, having
to look through a long list of matches is tedious.  Let's extend
these commands to allow specification of (input) argument types
to let such results be trimmed down.  Each additional argument
is treated the same as the pattern argument of \dT and matched
against the appropriate argument's type name.

While at it, fix \dT (and these new options) to recognize the
usual notation of "foo[]" for "the array type over foo", and
to handle the special abbreviations allowed by the backend
grammar, such as "int" for "integer".

Greg Sabino Mullane, revised rather significantly by me

Discussion: https://postgr.es/m/CAKAnmmLF9Hhu02N+s7uAyLc5J1xZReg72HQUoiKhNiJV3_jACQ@mail.gmail.com
This commit is contained in:
Tom Lane
2021-04-07 23:02:16 -04:00
parent f57a2f5e03
commit a3027e1e7f
8 changed files with 374 additions and 30 deletions

View File

@ -72,6 +72,9 @@ static backslashResult exec_command_copyright(PsqlScanState scan_state, bool act
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch);
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch,
const char *cmd);
static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd,
const char *pattern,
bool show_verbose, bool show_system);
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch,
PQExpBuffer query_buf, PQExpBuffer previous_buf);
static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
@ -790,7 +793,8 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
case 'p':
case 't':
case 'w':
success = describeFunctions(&cmd[2], pattern, show_verbose, show_system);
success = exec_command_dfo(scan_state, cmd, pattern,
show_verbose, show_system);
break;
default:
status = PSQL_CMD_UNKNOWN;
@ -811,7 +815,8 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
success = listSchemas(pattern, show_verbose, show_system);
break;
case 'o':
success = describeOperators(pattern, show_verbose, show_system);
success = exec_command_dfo(scan_state, cmd, pattern,
show_verbose, show_system);
break;
case 'O':
success = listCollations(pattern, show_verbose, show_system);
@ -951,6 +956,45 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
return status;
}
/* \df and \do; messy enough to split out of exec_command_d */
static bool
exec_command_dfo(PsqlScanState scan_state, const char *cmd,
const char *pattern,
bool show_verbose, bool show_system)
{
bool success;
char *arg_patterns[FUNC_MAX_ARGS];
int num_arg_patterns = 0;
/* Collect argument-type patterns too */
if (pattern) /* otherwise it was just \df or \do */
{
char *ap;
while ((ap = psql_scan_slash_option(scan_state,
OT_NORMAL, NULL, true)) != NULL)
{
arg_patterns[num_arg_patterns++] = ap;
if (num_arg_patterns >= FUNC_MAX_ARGS)
break; /* protect limited-size array */
}
}
if (cmd[1] == 'f')
success = describeFunctions(&cmd[2], pattern,
arg_patterns, num_arg_patterns,
show_verbose, show_system);
else
success = describeOperators(pattern,
arg_patterns, num_arg_patterns,
show_verbose, show_system);
while (--num_arg_patterns >= 0)
free(arg_patterns[num_arg_patterns]);
return success;
}
/*
* \e or \edit -- edit the current query buffer, or edit a file and
* make it the query buffer