package example.gemstone.sqlfabric.callbacks;
import java.sql.*;
import javax.sql.*;
import javax.sql.rowset.*;
import com.gemstone.gemfire.LogWriter;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.sqlfabric.callbacks.RowLoader;
import com.sun.rowset.CachedRowSetImpl;
public class ExampleRowLoader implements RowLoader{
private final LogWriter log = CacheFactory.getAnyInstance().getLogger();
public static ExampleRowLoader create() {
return new ExampleRowLoader();
}
public Object getRow(String schema, String table, Object[] params) {
log.entering("example.gemstone.sqlfabric.callbacks.ExampleLoader", "init()");
if (log.infoEnabled()) {
log.info("ExampleRowLoader invoked to fetch from schema <" + schema + "> on table <" + table + ">.");
for(int i = 0; i < params.length; i++) {
log.info(" primary key element " + i + ": " + params[i]);
}
}
switch ((Integer)params[0]){
case 1:
Object[] result1 = new Object[3];
result1[0] = params[0];
result1[1] = params[1];
result1[2] = "Success!";
return result1;
case 2:
ResultSet result2 = simulatedResultSet(params);
return result2;
default:
return null;
}
}
@Override
public void init(Object[] arg0) {
log.entering("example.gemstone.sqlfabric.callbacks.ExampleLoader", "init()");
if (log.infoEnabled()) {
log.info("ExampleRowLoader initialized.");
for (int i = 0; i < arg0.length; i++) {
log.info(" initialization arg " + i + ": " + arg0[i] );
}
}
}
protected ResultSet simulatedResultSet(Object[] params) {
try {
CachedRowSet crs = new CachedRowSetImpl();
crs.setType(RowSet.TYPE_SCROLL_INSENSITIVE);
RowSetMetaDataImpl rsmdi = new RowSetMetaDataImpl();
rsmdi.setColumnCount(3);
rsmdi.setColumnName(1, "id");
rsmdi.setColumnName(2, "name");
rsmdi.setColumnName(3, "result");
rsmdi.setNullable(1, ResultSetMetaData.columnNoNulls);
rsmdi.setNullable(2, ResultSetMetaData.columnNoNulls);
rsmdi.setNullable(3, ResultSetMetaData.columnNullable);
rsmdi.setColumnType(1, Types.INTEGER);
rsmdi.setColumnType(2, Types.VARCHAR);
rsmdi.setColumnType(3, Types.VARCHAR);
crs.setMetaData(rsmdi);
crs.moveToInsertRow();
crs.updateInt(1, (Integer) params[0]);
crs.updateString(2, (String) params[1]);
crs.updateString(3, "Sucess!");
crs.insertRow();
crs.moveToCurrentRow();
crs.beforeFirst();
return crs;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
ExampleRowLoader loader = ExampleRowLoader.create();
loader.init(new Object[0]);
Object[] params = new Object[2];
params[0] = 2;
params[1] = "test";
loader.getRow("Schema","Table", params);
}
}