git.postgresql.org Git - postgresql.git/commitdiff (original) (raw)
+
+ \getenv psql_var env_var
+
+
+ Gets the value of the environment
+ variable env_var
+ and assigns it to the psql
+ variable psql_var.
+ If env_var is
+ not defined in the psql process's
+ environment, psql_var
+ is not changed. Example:
+
+=> \getenv home HOME
+=> \echo :home
+/home/postgres
+
+
+
\gexec
bool active_branch,
const char *cmd);
static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch);
+static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch,
+ const char *cmd);
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);
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, "getenv") == 0)
+ status = exec_command_getenv(scan_state, active_branch, cmd);
else if (strcmp(cmd, "gexec") == 0)
status = exec_command_gexec(scan_state, active_branch);
else if (strcmp(cmd, "gset") == 0)
return status;
}
+/*
+ * \getenv -- set variable from environment variable
+ */
+static backslashResult
+exec_command_getenv(PsqlScanState scan_state, bool active_branch,
+ const char *cmd)
+{
+ bool success = true;
+ if (active_branch)
+ {
+ char *myvar = psql_scan_slash_option(scan_state,
+ OT_NORMAL, NULL, false);
+ char *envvar = psql_scan_slash_option(scan_state,
+ OT_NORMAL, NULL, false);
+ if (!myvar || !envvar)
+ {
+ pg_log_error("\\%s: missing required argument", cmd);
+ success = false;
+ }
+ else
+ {
+ char *envval = getenv(envvar);
+ if (envval && !SetVariable(pset.vars, myvar, envval))
+ success = false;
+ }
+ free(myvar);
+ free(envvar);
+ }
+ else
+ ignore_slash_options(scan_state);
+ return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
+}
/*
* \gexec -- send query and execute each field of result
*/
(1 row)
\unset FETCH_COUNT
+-- \setenv, \getenv
+-- ensure MYVAR isn't set
+\setenv MYVAR
+-- in which case, reading it doesn't change the target
+\getenv res MYVAR
+\echo :res
+:res
+-- now set it
+\setenv MYVAR 'environment value'
+\getenv res MYVAR
+\echo :res
+environment value
-- show all pset options
\pset
border 1
\unset FETCH_COUNT
+-- \setenv, \getenv
+-- ensure MYVAR isn't set
+\setenv MYVAR
+-- in which case, reading it doesn't change the target
+\getenv res MYVAR
+\echo :res
+-- now set it
+\setenv MYVAR 'environment value'
+\getenv res MYVAR
+\echo :res
-- show all pset options
\pset