Queries suffer from being rebuilt every time they are executed. Especially when the same filter has to be re-used more than once, there should be a more effective way to change simply the value of the filter, not the filter itself.
One solution for this problem is to use user-defined values within filters. These parameters stand for variable parts (the values) in filters and can be changed without rebuilding the entire filter expression again. Another use of these parameters is to insert non-standardized values (like data values) into the filter.
To define parameters you can use any identifier. But you should avoid using attribute names of the classes that are part of the filter expression.
The following example uses a parameter for a query:
// create query
Query query = pm.newQuery(Person.class);
// set filter with parameters
query.setFilter("(id > paramMinId) && (id < paramMaxId)");
// define parameter
query.declareParameters("Integer paramMaxId, Integer paramMinId");
// execute query with concrete filter values
Collection col = (Collection)query.execute(new Integer(200), new Integer(50));
The query is created for the class Person without a filter. The filter is applied using the setFilter() method instead, although it is possible to define the filter immediately within the newQuery() method.
The filter text contains the two parameters paramMinId and paramMaxId . Note that as long as these two identifiers are not changed into parameters, the identifiers are treated as normal text.
Using the method declareParameter() you can define the two parameters and declare their types. The type definition is made with the same method as known from Java. Multiple definitions are separated by a comma.
When running the query, the defined parameters have to be filled with values. The order of the parameters within the execute() method has to be the same as the order in declareParameters() . Note that - according to the JDO specification- the execute() method that takes up to four parameters or an array .
You can reuse the query by passing different values to the execute() method. By doing that, the query does not have to be parsed again by Signsoft intelliBO and your database server, if prepared statements are supported by your database.