Friday, June 8, 2012

Parameterized Query Example : ATG

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 you would need 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();
QueryBuilder 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);


In the first example, we have to create a new constant QueryExpression if we want to change the name from Jerry to Phil. This means we must also create a new instance of a comparison Query to use the new QueryExpression. In the second example, we can increase efficiency by creating just one Query object, and changing the value of the name we want in an Object array that is passed to the executeQuery method. This also allows you to cache a Query in your internal application (above the query cache layer), and pass in varying parameter values at execution time.

No comments:

Popular Posts