The following is an example of how you might use a parameterized query. Note that error handling is not dealt with in these examples.

Suppose you wanted to create a query like this:

firstName  = 'Jerry'

Then, you want to change that to

firstName = 'Phil'

Query Example without Parameters

The first example shows how to do this without the use of parameters:

// Get the repository through our made up getRepository( ) call
Repository rep = getRepository();
RepositoryItemDescriptor desc = rep.getItemDescriptor("user");
RepositoryView view = desc.getRepositoryView();
QueryBuilder qb = view.getQueryBuilder();

// Build our first Query
// firstName = 'Jerry'
QueryExpression firstNameProp = qb.createPropertyQueryExpression("firstName");
QueryExpression jerryValue = qb.createConstantQueryExpression
(new String("Jerry"));
Query firstNameQuery = qb.createComparisonQuery(firstNameProp, jerryValue,
 QueryBuilder.EQUALS);

// Execute our first Query
RepositoryItem[] jerryItems = view.executeQuery(firstNameQuery);

// Set up our second Query now
QueryExpression philValue = qb.createConstantQueryExpression(new String("Phil"));
firstNameQuery = qb.createComparisonQuery(firstNameProp, philValue,
QueryBuilder.EQUALS);

// Execute our second Query
RepositoryItem[] philItems = view.executeQuery(firstNameQuery);
Query Example with Parameters

With the use of parameters in your queries, you can create a reusable Query as in the example that follows. Note that the view used is a ParameterSupportView instead of a RepositoryView:

// Get the repository through our made up getRepository( ) call
Repository rep = getRepository();
RepositoryItemDescriptor desc = rep.getItemDescriptor("user");
// Our RepositoryView is a ParameterSupportView this time, so we know it supports
// parameters in Queries
// Note – this assumes we have advanced knowledge that this view is an instance of
// a ParameterSupportView
ParameterSupportView view = (ParameterSupportView)desc.getRepositoryView();
ParameterSupportQueryBuilder qb = view.getQueryBuilder();

// Builder our first Query up
// firstName = 'Jerry'
QueryExpression firstNameProp = qb.createPropertyQueryExpression("firstName");
QueryExpression parameterValue = qb.createParameterQueryExpression();
Query firstNameQuery = qb.createComparisonQuery
            (firstNameProp, parameterValue, QueryBuilder.EQUALS);

// Execute our first Query
Object[] args = new Object[1];
args[0] = new String("Jerry");
RepositoryItem[] jerryItems = view.executeQuery(firstNameQuery, args);

// Set up our second Query now
args[0] = new String("Phil");
RepositoryItem[] philItems = view.executeQuery(firstNameQuery, args);

The first example creates a new constant QueryExpression in order to change the name from Jerry to Phil. This also requires a new instance of a comparison Query to use the new QueryExpression. The second example increases efficiency by creating just one Query object, and changing the value of the desired name in an Object array that is passed to the executeQuery method. This also lets you cache a Query in your internal application (above the query cache layer), and pass in varying parameter values at execution time.