mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
Provide a test for variable existence in psql
"\if :{?variable_name}" will be translated to "\if TRUE" if the variable exists and "\if FALSE" otherwise. Thus it will be possible to execute code conditionally on the existence of the variable, regardless of its value. Fabien Coelho, with some review by Robins Tharakan and some light text editing by me. Discussion: https://postgr.es/m/alpine.DEB.2.20.1708260835520.3627@lancre
This commit is contained in:
@@ -745,9 +745,13 @@ other .
|
||||
PQUOTE_SQL_IDENT);
|
||||
}
|
||||
|
||||
:\{\?{variable_char}+\} {
|
||||
psqlscan_test_variable(cur_state, yytext, yyleng);
|
||||
}
|
||||
|
||||
/*
|
||||
* These rules just avoid the need for scanner backup if one of the
|
||||
* two rules above fails to match completely.
|
||||
* three rules above fails to match completely.
|
||||
*/
|
||||
|
||||
:'{variable_char}* {
|
||||
@@ -762,6 +766,17 @@ other .
|
||||
ECHO;
|
||||
}
|
||||
|
||||
:\{\?{variable_char}* {
|
||||
/* Throw back everything but the colon */
|
||||
yyless(1);
|
||||
ECHO;
|
||||
}
|
||||
:\{ {
|
||||
/* Throw back everything but the colon */
|
||||
yyless(1);
|
||||
ECHO;
|
||||
}
|
||||
|
||||
/*
|
||||
* Back to backend-compatible rules.
|
||||
*/
|
||||
@@ -1442,3 +1457,28 @@ psqlscan_escape_variable(PsqlScanState state, const char *txt, int len,
|
||||
psqlscan_emit(state, txt, len);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
psqlscan_test_variable(PsqlScanState state, const char *txt, int len)
|
||||
{
|
||||
char *varname;
|
||||
char *value;
|
||||
|
||||
varname = psqlscan_extract_substring(state, txt + 3, len - 4);
|
||||
if (state->callbacks->get_variable)
|
||||
value = state->callbacks->get_variable(varname, PQUOTE_PLAIN,
|
||||
state->cb_passthrough);
|
||||
else
|
||||
value = NULL;
|
||||
free(varname);
|
||||
|
||||
if (value != NULL)
|
||||
{
|
||||
psqlscan_emit(state, "TRUE", 4);
|
||||
free(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
psqlscan_emit(state, "FALSE", 5);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user