Sometimes it is necessary to execute a query on functions associated with an application that is already deployed. Rather than take the application offline to create a new query, ALDSP provides the PreparedExpression class, which lets you create and run ad hoc queries on deployed applications.
After completing this lesson, you will be able to:
ALDSP includes a PreparedExpression class that lets you build an ad hoc query using remote data sources, and then execute it using the Mediator API or ALDSP Control. Using the methods within the PreparedExpression class, you can build queries on top of existing XDS functions belonging to applications already deployed on an active local or remote server domain.
The process for running an ad hoc query is as follows:
The first steps in creating an ad hoc query are to instantiate a StringBuffer and the PreparedExpression class. For the latter instance, you use the prepareExpression method of the DataServiceFactory class, which accepts three parameters:
PreparedExpression pe = DataServiceFactory.prepareExpression(
getInitialContext(),
"Evaluation",
xquery.toString()
);
import com.bea.ld.dsmediator.client.DataServiceFactory;
import com.bea.ld.dsmediator.client.PreparedExpression;
import com.bea.xml.XmlObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import weblogic.jndi.Environment;
Note: | You can also import the necessary Java classes by first adding the code specified below, and then pressing Alt + Enter. |
public static InitialContext getInitialContext() throws NamingException {
Environment env = new Environment();
env.setProviderUrl("t3://localhost:7001");
env.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
env.setSecurityPrincipal("weblogic");
env.setSecurityCredentials("weblogic");
return new InitialContext(env.getInitialContext().getEnvironment());
}
public static void main (String args[]) {
System.out.println("========== Ad Hoc Client =============");
try {
} catch (Exception e) {
e.printStackTrace();
}
}
try {:
StringBuffer xquery = new StringBuffer();
xquery.append("declare variable $p_firstname as xs:string external; \n");
xquery.append("declare variable $p_lastname as xs:string external; \n");
xquery.append("declare namespace ns1=\"ld:DataServices/MyQueries/XQueries\"; \n");
xquery.append("declare namespace ns0=\"ld:DataServices/CustomerDB/CUSTOMER\"; \n\n");
xquery.append("<ns1:RESULTS> \n");
xquery.append("{ \n");
xquery.append(" for $customer in ns0:CUSTOMER() \n");
xquery.append(" where ($customer/FIRST_NAME eq $p_firstname \n");
xquery.append(" and $customer/LAST_NAME eq $p_lastname) \n");
xquery.append(" return \n");
xquery.append(" $customer \n");
xquery.append(" } \n");
xquery.append("</ns1:RESULTS> \n");
PreparedExpression pe = DataServiceFactory.prepareExpression(
getInitialContext(), "Evaluation", xquery.toString());
After you create an instance of the PreparedExpression class, you need to specify the parameters that will be passed when the ad hoc query is submitted. To pass parameters, you use one or more bind<DataType> methods, such as bindString and bindInt.
pe.bindString(new QName("p_firstname"), "Jack");
pe.bindString(new QName("p_lastname"), "Black");
XmlObject obj = pe.executeQuery();
System.out.println(obj.toString());
You are now ready to test the ad hoc query, which is set to return information for Jack Black.
import com.bea.ld.dsmediator.client.DataServiceFactory;
import com.bea.ld.dsmediator.client.PreparedExpression;
import com.bea.xml.XmlObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import weblogic.jndi.Environment;
public class AdHocQuery
{
public static InitialContext getInitialContext() throws NamingException {
Environment env = new Environment();
env.setProviderUrl("t3://localhost:7001");
env.setInitialContextFactory("weblogic.jndi.WLInitialContextFactory");
env.setSecurityPrincipal("weblogic");
env.setSecurityCredentials("weblogic");
return new InitialContext(env.getInitialContext().getEnvironment());
}
public static void main (String args[]) {
System.out.println("==================== Ad Hoc Client ====================");
try {
StringBuffer xquery = new StringBuffer();
xquery.append("declare variable $p_firstname as xs:string external; \n");
xquery.append("declare variable $p_lastname as xs:string external; \n");
xquery.append("declare namespace ns1=\"ld:DataServices/MyQueries/XQueries\"; \n");
xquery.append("declare namespace ns0=\"ld:DataServices/CustomerDB/CUSTOMER\"; \n\n");
xquery.append("<ns1:RESULTS> \n");
xquery.append("{ \n");
xquery.append(" for $customer in ns0:CUSTOMER() \n");
xquery.append(" where ($customer/FIRST_NAME eq $p_firstname \n");
xquery.append(" and $customer/LAST_NAME eq $p_lastname) \n");
xquery.append(" return \n");
xquery.append(" $customer \n");
xquery.append(" } \n");
xquery.append("</ns1:RESULTS> \n");
PreparedExpression pe = DataServiceFactory.prepareExpression(getInitialContext(), "Evaluation", xquery.toString());
pe.bindString(new QName("p_firstname"), "Jack");
pe.bindString(new QName("p_lastname"), "Black");
XmlObject results = pe.executeQuery();
System.out.println(results);
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this lesson, you learned how to: