Extending user-defined classes from generated implementation classes

You can write classes that are extended by implementation classes that the pureQuery Generator generates.

About this task

The pureQuery Generator utility generates classes that implement both an interface that declares annotated methods and extend an implementation of the Data interface. Because applications generally do not need access to the many methods of the Data interface, at run time the methods in DataFactory that instantiate implementation classes for particular annotated-method interfaces return instances of those interfaces.

For example, if one of these DataFactory methods instantiates an implementation of the interface MyInterface, and myObject is set to the return value of the method, then myObject is of type MyInterface. For your application to access the Data methods in myObject, the application must cast myObject to a Data object.

When the pureQuery Generator utility generates an implementation class, by default that class extends an implementation of the Data interface that is named BaseData. The implementation class can, however, extend a different class that itself extends BaseData.

You might want an implementation class to do this for the any of the following reasons:
  • There are a few methods in Data that you would like to be able to access in your generated implementation class without having to cast the class as type Data.
  • You would like to access one of the methods in Data by means of a different name.
  • There is at least one method that you want to implement yourself (rather than having its implementation generated) and have available in the implementation class that the Generator utility generates. For example, you might want to implement a method yourself if you want the implementation to be different than the generated implementation would be, or if the method is not a method that can be generated as an annotated method.
Attention: The class that extends BaseData must not override any methods that are implemented in BaseData.

Procedure

To extend your class from an implementation class that the Generator utility generates:

  1. Create a class that extends com.ibm.pdq.runtime.generator.BaseData and defines methods that you want to be available from the generated implementation class.
  2. Run the pureQuery Generator, supplying the interface for which you want to create an implementation class and the name of the class that you want to extend.
    1. Supply the name of the annotated-method interface that you want to generate an implementation class for.
    2. With the baseDataOverride option for the pureQuery Generator, supply the name of the class that you want the generated implementation class to extend.

Example

Suppose that you want to add three methods to the implementation class that the pureQuery Generator generates for an interface that declares annotated methods. The three additional methods perform these functions:
  • Heterogeneous Batch
  • Dynamic query SQL returning a "Bean" array
  • Dynamic insert, update, delete, DDL SQL
You create the following interface to contain the three methods:
package customer;
public interface CommonInterface
{
  public void startHeterogeneousBatch (); // this method "renames" the Data Interface method
  public int[][] endHeterogeneousBatch (); // this method "renames" the Data Interface method
  public int dynamicSQLUpdate (String updateSQL, Object... parameters); // this method "renames" the Data Interface method
  public <T> T[] dynamicQueryArray (String sql, Class<T> returnClass, Object... parameters); // this method "renames" the Data Interface method
  public void commit ();
  public void rollback ();
  // any other Data Interface method can be coded here also
}
You implement this interface with the following class:
package customer;

public class BaseData extends com.ibm.pdq.runtime.generator.BaseData implements CommonInterface
{
  public <T> T[] dynamicQueryArray (String sql, Class<T> returnClass, Object... parameters)
  {
    return queryArray (sql, returnClass, parameters);
  }
  public int dynamicSQLUpdate (String updateSQL, Object... parameters)
  {
    return update (updateSQL, parameters);
  }
  public void startHeterogeneousBatch ()
  {
    startBatch (heterogeneousModify__);
    return;
  }
  public int[][] endHeterogeneousBatch ()
  {
    return endBatch ();
  }
}

Notice that this class extends com.ibm.pdq.runtime.generator.BaseData.

Now, suppose that you create an interface that declares an annotated method:
import com.ibm.pdq.annotation.Update;

public interface AutoGeneratedKeysInterface
{
  @Update(sql = "insert into MYEMPLOYEE (name, salary, deptno) values(:name, :salary, :deptno)")
  int createEmployee (MyEmployeeBean bean);
}
You modify the interface so that it extends CommonInterface:
import com.ibm.pdq.annotation.Update;

public interface AutoGeneratedKeysInterface extends customer.CommonInterface
{
  @Update(sql = "insert into MYEMPLOYEE (name, salary, deptno) values(:name, :salary, :deptno)")
  int createEmployee (MyEmployeeBean bean);
}

When you use the pureQuery Generator to generate the implementation class for this interface, for the option baseDataOverride you specify customer.BaseData.


Feedback