Example Java iterator function
The UDREnv interface provides all necessary methods and constants. A Java™ UDR can return only one value; therefore, there can be only one column in the virtual column list in the FROM clause.
The following example demonstrates how to write a Java iterator function and use it in FROM clause; relevant DataBlade® API and iterator states are highlighted.
The iterator UDR jenv_iter() takes an integer parameter and returns a row of
CHAR(40) columns. The parameter passed in determines the number of rows it
returns.
public interface UDREnv
{
...
// for maintaining state across UDR invocations
void setUDRState(Object state);
Object getUDRState();
// for set/iterator processing
public static final int UDR_SET_INIT = 1;
public static final int UDR_SET_RETONE = 2;
public static final int UDR_SET_END = 3;
int getSetIterationState();
void setSetIterationIsDone(boolean value);
...
}
import java.lang.*;
import java.sql.*;
import com.informix.udr.*;
import informix.jvp.*;
public class Env
{
public int count;
//
// test UDR meta
//
public static String envTest1(int i, String xchar, String
xvchar, String xlvarchar)
throws SQLException
{
UDREnv env = UDRManager.getUDREnv();
String res = env.getName() + "#" +
env.getReturnTypeName() + "#";
String param[] = env.getParamTypeName();
for (int j = 0; j < param.length; ++ j)
res += param[j] + "#";
res += i + xchar + xvchar + xlvarchar;
return res;
}
public static String envTest2(int i, String s[])
throws SQLException
{
UDREnv env = UDRManager.getUDREnv();
UDRLog log = env.getLog();
String res = env.getName() + "#" +
env.getReturnTypeName() + "#";
String param[] = env.getParamTypeName();
for (int j = 0; j < param.length; ++ j)
res += param[j] + "#";
res += i;
log.log(res);
s[0] = res;
return res;
}
//
//test env state, iterator, log, traceable, and
properties
//
public static String envIter(int num)
throws SQLException
{
UDREnv env = UDRManager.getUDREnv();
UDRLog log = env.getLog();
UDRTraceable tr = env.getTraceable();
JVPProperties pr = env.getProperties();
int iter = env.getSetIterationState();
Env state = (Env)env.getUDRState();
if (iter == UDREnv.UDR_SET_INIT)
{
state = new Env();
state.count = num;
env.setUDRState(state);
log.log("SET INIT" + state.count + " " +
state.toString());
tr.tracePrint("UDR.ENVITER", 0, "SET INIT");
env.setSetIterationIsDone(false);
pr.setProperty("ENVITERPROP", "AFTER INIT");
return "INIT";
}
else if (iter == UDREnv.UDR_SET_END)
{
log.log("SET DONE");
tr.tracePrint("UDR.ENVITER", 0, "SET DONE");
env.setSetIterationIsDone(true);
return "DONE";
}
else if (iter == UDREnv.UDR_SET_RETONE)
{
log.log("SET RETONE" + state.count + " " +
state.toString());
tr.tracePrint("UDR.ENVITER", 0, "SET RETONE");
String prv = pr.getProperty("ENVITERPROP");
if (state.count <= 0)
env.setSetIterationIsDone(true);
else
env.setSetIterationIsDone(false);
-- state.count;
pr.setProperty("ENVITERPROP", "AFTER RETONE" +
(state.count + 1));
return new String("ELEMENT " + (state.count + 1) );
//+ prv);
}
else
throw new SQLException("Bad iter code");
}
}
The following statement creates the Java iterator UDR, jenv_iter().
create function jenv_iter(int)
returning char(40)
with (class = "jvp", iterator)
external name ˋEnv.envIter(int)'
language java;