User-defined persisted queries

You can create a PropertyQuery or GraphQuery and then persist it in the registry, specifying a name of your choice. You can then execute the query by referencing the name.

The query is precompiled and persisted, along with the original XPath expression, for improved performance when the query is executed. However, if the precompiled query exceeds 1020 bytes, it is not persisted, resulting in a small affect on performance because the XPath expression is compiled each time it is executed.

Example

In this example, a PropertyQuery is persisted with the name "MyPropertyQuery", and then executed.
try {
    // define the query
    PropertyQuery query = (PropertyQuery)DataFactory.INSTANCE.create(
               TypeConstants.SR_URI, TypeConstants.TYPE_PROPERTYQUERY);
		
    // specify XPath to retrieve all the XMLDocuments in the registry
    query.setQueryExpression("/WSRR/XMLDocument");

    // specify the properties to be returned
    BSRSDOHelper.INSTANCE.addProperty(query, "p1", "description");
    BSRSDOHelper.INSTANCE.addProperty(query, "p2", "name");

    // persist the query with a user-defined name
    query.setName("MyPropertyQuery");
    String bsrURI = client.create(query);

    // execute the query
    List<PropertyQueryResult> propertyQueryResults = (List<PropertyQueryResult>)client.executeQuery(query);
	
    // process each PropertyQueryResultObject found in the list
    for (PropertyQueryResult propertyQueryResult : propertyQueryResults) {
	
        // get the name
        String name = BSRSDOHelper.INSTANCE.getPropertyQueryResultValue
                                           (propertyQueryResult,"name");

        // get the description
        String description = BSRSDOHelper.INSTANCE.getPropertyQueryResultValue
                                           (propertyQueryResult,"description");

    }
	
} catch (ServiceRegistryException e) {
    // handle exception
}

Passing parameters to a user-defined query

The following query expression finds all XMLDocuments with a given name and version, where the name and version values are passed as parameters when the query is executed:
query.setQueryExpression("/WSRR/XMLDocument[@name='%1' and @version='%2']");
The parameters are passed as arguments to the executeNamedQuery method call; for example:
List graphQueryResults = client.executeNamedQuery("MyGraphQuery", new Object[] { "MyXML", "6.1" });