Reference Guide > Custom Procedure Examples > Example 1: Simple Query
 
Example 1: Simple Query
This custom procedure participates in the parent transaction, and invokes a query using the execution environment.
package proc;
 
import com.compositesw.extension.*;
import java.sql.*;
 
public class SimpleQuery
 
implements CustomProcedure
 
{
private ExecutionEnvironment qenv;
private ResultSet resultSet;
 
public SimpleQuery() { }
 
/**
* This is called once just after constructing the class. The
* environment contains methods used to interact with the server.
*/
 
public void initialize(ExecutionEnvironment qenv) {
this.qenv = qenv;
}
 
/**
* Called during introspection to get the description of the input
* and output parameters. Should not return null.
*/
 
public ParameterInfo[] getParameterInfo() {
 
return new ParameterInfo[] {
new ParameterInfo("id", Types.INTEGER, DIRECTION_IN),
new ParameterInfo("result", TYPED_CURSOR, DIRECTION_OUT,
new ParameterInfo[] {
new ParameterInfo("Id", Types.INTEGER, DIRECTION_NONE),
new ParameterInfo("FirstName", Types.VARCHAR, DIRECTION_NONE),
new ParameterInfo("LastName", Types.VARCHAR, DIRECTION_NONE),
new ParameterInfo("CompanyName", Types.VARCHAR, DIRECTION_NONE),
new ParameterInfo("PhoneNumber", Types.VARCHAR, DIRECTION_NONE),
}
)
};
}
 
/**
* Called to invoke the stored procedure. Will only be called a
* single time per instance. Can throw CustomProcedureException or
* SQLException if there is an error during invoke.
*/
public void invoke(Object[] inputValues)
throws CustomProcedureException, SQLException
{
resultSet = qenv.executeQuery(
"SELECT " +
"CustomerID AS Id, " +
"ContactFirstName AS FirstName, " +
"ContactLastName AS LastName, " +
"CompanyName AS CompanyName, " +
"PhoneNumber AS PhoneNumber FROM " +
"/shared/tutorial/sources/ds_orders/customers WHERE CustomerID=" +
inputValues[0],
null);
}
/**
* Called to retrieve the number of rows that were inserted,
* updated, or deleted during the execution of the procedure. A
* return value of -1 indicates that the number of affected rows is
* unknown. Can throw CustomProcedureException or SQLException if
* there is an error when getting the number of affected rows.
*/
public int getNumAffectedRows() {
return 0;
}
 
/**
* Called to retrieve the output values. The returned objects
* should obey the Java to SQL typing conventions as defined in the
* table above. Output cursors can be returned as either
* CustomCursor or java.sql.ResultSet. Can throw
* CustomProcedureException or SQLException if there is an error
* when getting the output values. Should not return null.
*/
 
public Object[] getOutputValues() {
return new Object[] { resultSet };
}
/**
* Called when the procedure reference is no longer needed. Close
* can be called without retrieving any of the output values (such
* as cursors) or even invoking, so this needs to do any remaining
* cleanup. Close can be called concurrently with any other call
* such as "invoke" or "getOutputValues". In this case, any pending
* methods should immediately throw a CustomProcedureException.
*/
 
public void close() throws SQLException {
if (resultSet != null) {
resultSet.close();
}
}
//
// Introspection methods
//
/**
* Called during introspection to get the short name of the stored
* procedure. This name can be overridden during configuration.
* Should not return null.
*/
 
public String getName() {
return "SimpleQuery";
}
 
/**
* Called during introspection to get the description of the stored
* procedure. Should not return null.
*/
 
public String getDescription() {
return "This procedure performs a simple query operation";
}
 
//
// Transaction methods
//
/**
* Returns true if the custom procedure uses transactions. If this
* method returns false then commit and rollback will not be called.
*/
 
public boolean canCommit() {
 
return false;
}
/**
* Commit any open transactions.
*/
 
public void commit() { }
 
/**
* Rollback any open transactions.
*/
 
public void rollback() { }
/**
* Returns true if the transaction can be compensated.
*/
 
public boolean canCompensate() {
return false;
}
 
/**
* Compensate any committed transactions (if supported).
*/
 
public void compensate(ExecutionEnvironment qenv) { }
}