mirror of
https://github.com/postgres/postgres.git
synced 2025-08-27 07:42:10 +03:00
Allow psql variable substitution to occur in backtick command strings.
Previously, text between backquotes in a psql metacommand's arguments was always passed to the shell literally. That considerably hobbles the usefulness of the feature for scripting, so we'd foreseen for a long time that we'd someday want to allow substitution of psql variables into the shell command. IMO the addition of \if metacommands has brought us to that point, since \if can greatly benefit from some sort of client-side expression evaluation capability, and psql itself is not going to grow any such thing in time for v10. Hence, this patch. It allows :VARIABLE to be replaced by the exact contents of the named variable, while :'VARIABLE' is replaced by the variable's contents suitably quoted to become a single shell-command argument. (The quoting rules for that are different from those for SQL literals, so this is a bit of an abuse of the :'VARIABLE' notation, but I doubt anyone will be confused.) As with other situations in psql, no substitution occurs if the word following a colon is not a known variable name. That limits the risk of compatibility problems for existing psql scripts; but the risk isn't zero, so this needs to be called out in the v10 release notes. Discussion: https://postgr.es/m/9561.1490895211@sss.pgh.pa.us
This commit is contained in:
@@ -48,13 +48,22 @@ typedef enum _promptStatus
|
||||
PROMPT_COPY
|
||||
} promptStatus_t;
|
||||
|
||||
/* Quoting request types for get_variable() callback */
|
||||
typedef enum
|
||||
{
|
||||
PQUOTE_PLAIN, /* just return the actual value */
|
||||
PQUOTE_SQL_LITERAL, /* add quotes to make a valid SQL literal */
|
||||
PQUOTE_SQL_IDENT, /* quote if needed to make a SQL identifier */
|
||||
PQUOTE_SHELL_ARG /* quote if needed to be safe in a shell cmd */
|
||||
} PsqlScanQuoteType;
|
||||
|
||||
/* Callback functions to be used by the lexer */
|
||||
typedef struct PsqlScanCallbacks
|
||||
{
|
||||
/* Fetch value of a variable, as a pfree'able string; NULL if unknown */
|
||||
/* Fetch value of a variable, as a free'able string; NULL if unknown */
|
||||
/* This pointer can be NULL if no variable substitution is wanted */
|
||||
char *(*get_variable) (const char *varname, bool escape,
|
||||
bool as_ident, void *passthrough);
|
||||
char *(*get_variable) (const char *varname, PsqlScanQuoteType quote,
|
||||
void *passthrough);
|
||||
/* Print an error message someplace appropriate */
|
||||
/* (very old gcc versions don't support attributes on function pointers) */
|
||||
#if defined(__GNUC__) && __GNUC__ < 4
|
||||
|
Reference in New Issue
Block a user