UDX environment

The UDX environment consists of a list of one or more variable name and value pairs that you can specify for a UDSF, UDAF, or UDTF when you register it. Using environment variables, you can conditionalize the UDX behavior in the UDX definition.

The environment variables simplify the process for changing the behavior of the UDX when necessary. To change a variable, you alter the UDX to specify new variables, new values, or to clear the variables. Although you can define the variable values within the source code for the UDX, changing the values would require you to edit the source code, recompile, and reregister the UDX to implement the new behavior. Similarly, if you defined variables as an argument to the UDX when you invoke it, changing the variable would require changes to the SQL query that invokes the UDX.

For example, assume that you have a UDSF that converts currency values from US dollars to euros. If you hardcode an exchange rate variable within the UDSF source code, you would need to update the source code whenever the exchange rate changes, then recompile and reregister the UDSF. Instead, you can use an environment variable to define the exchange rate for the UDSF when you register it, as in the following example:
CREATE OR REPLACE FUNCTION usdToEurosFunc(int) RETURNS integer
LANGUAGE CPP PARAMETER STYLE NPSGENERIC ENVIRONMENT 'USD_EURO'='0.7268'
EXTERNAL 'CusdToEuros';
When you update the exchange rate, you can reregister the UDX, as shown in bold:
CREATE OR REPLACE FUNCTION usdToEurosFunc(int) RETURNS integer
LANGUAGE CPP PARAMETER STYLE NPSGENERIC ENVIRONMENT 'USD_EURO'='0.8241'
EXTERNAL 'CusdToEuros';

You can specify and alter the environment variables by using the CREATE OR REPLACE commands for UDXs. You can use the ALTER command to alter variable values and to clear the environment of all variable settings by using the NO ENVIRONMENT syntax. To alter an existing set of one or more environment pairs, you must specify all the environment settings; the ALTER command replaces the current list with the list specified in the ALTER command.

Within the UDSF, you can retrieve the UdxEnvironment class to obtain and use the environment variables:
UdxEnvironment* getEnvironment()
The UDX environment is a structure of UdxEnvironmentEntry values that store each name-value pair that you specify when you register the UDX. The environment variables are available to the UDX constructor. You can use the following methods to obtain information about the entries in the environment and to obtain a specific entry in the environment by its name, its position (key), or its value:
  • The getNumEntries() method returns the number of environment variables in the UdxEnvironment object. For example:
       int numEntries = env->getNumEntries();
  • The findEntry() method takes an input string and matches it against the variable names. It returns the key number of the first matching entry in the environment structure, or -1 if the string is not found.
  • The getEntry() method takes an input index or variable name value and returns the matching UdxEnvironmentEntry or a null value if not found.
  • The getKey() method returns the matching environment variable name from a UdxEnvironmentEntry.
  • The getValue() method returns the matching environment variable value from a UdxEnvironmentEntry.