In this article, I’ll prepare and configure Sybase Adaptive Server Enterprise (ASE) for XQL. I’ll cover installing XML and XQL in Sybase ASE, including performance considerations, and discuss basic capabilities available on both the client and server.
Preparing ASE for XQL
In order to use XQL on the server, you must first enable XML. Of course, this assumes you’ve already enabled the Java Virtual Machine (VM) on the server. So, with the Java VM already installed, run the installjava utility. The installjava utility copies a JAR file (or even a JAR in a ZIP file) into Adaptive Server and makes the Java classes available for use in the current database. The syntax is:
installjava
-f file_name
[-new | -update ]
…
Where:
- · The file_name is the name of the JAR file you’re installing in the server.
- · New informs the server this is a new file.
- · Update informs the server you’re updating an existing JAR file.
For more information about installjava, see the ASE Utility Guide.
To add support for XML in Adaptive Server, you must install the xml.zip and xerces.jar files. These files are located in the directories $SYBASE/ASE-12_5/lib/xml.zip and $SYBASE/ASE-12_5/lib/xerces.jar.
For example, to install xml.zip, enter:
installjava -Usa -P -Sserver_name -f $SYBASE/ASE-12_5/lib/xml.zip
To install xerces.jar, enter:
installjava -Usa -P -Sserver_name -f $SYBASE/ASE-12_5/lib/xerces.jar
Note: To install xerces.jar in a database, you must increase the size of tempdb by 10 MB.
Memory requirements for running the query engine inside Adaptive Server
Depending on the size of the XML data you want to select and present as an XQL document, you may need to increase memory. For a typical XML document of 2 KB, Sybase recommends setting the configuration parameters in Java Services to the values shown in Table A. For more information on configuration parameters, see the Sybase Adaptive Server System Administration Guides.
Table A
|
Java Services memory parameters
Query structures that affect performance
The placement of the query() method in an XQL query will have a definite effect on processing. For example, Listing A contains an example of an XQL query designed to select all the books in which the author’s first name is Mary.
In Listing A, the query() method is invoked twice, once in the WHERE clause and once in the SELECT clause. This means the query executes twice and will more than likely be very slow for large documents.
However, you can save the result set in an object while executing the query in the WHERE clause and then restore the result in the SELECT clause.
Or, you can write a class like HoldString, which concatenates the results obtained from every invocation of com.sybase.xml.xql.Xql.query(), for each XML document in each row:
declare @result HoldString
select @result = new HoldString()
select @result>>get()
from XMLDAT
where
@result>>put(com.sybase.xml.xql.Xql.query
(“/bookstore/book[author/first-name= ‘Mary’]”, xmlcol))!=
convert(com.sybase.xml.xql.Xql,null)>>EmptyResult
It’s not wise to store the result set in the WHERE clause. Depending on the query and the data contained in the table, the query may not always execute the WHERE clause, so trying to retrieve its result in the SELECT clause may generate an erroneous result set. HoldString is just a hypothetical example of a class to achieve exactly this function.
Because Adaptive Server stores each document in a column of a given row, more than one row may satisfy the search criteria when the query scans a set of rows in the WHERE clause. If this occurs, the query returns a separate XML result document for each qualified row.
For example, if you create the following table:
create table XMLTAB ( xmlcol image)
insert XMLTAB values
( com.sybase.xml.xql.Xql.parse(<xml><A><B><C>c</C></B></A></xml>));
insert XMLTAB values
( com.sybase.xml.xql.Xql.parse(<xml><D><E><C>c</C></E></D></xml>));
and then execute this query:
select com.sybase.xml.xql.Xql.query(“//C”, xmlcol)
from XMLTAB
you would expect to get the following result set:
<xql_result>
<C>c</C>
<C>c</C>
</xql_result>
Instead, the result set returns the same row twice, once from the SELECT clause and once from the WHERE clause:
<xql_result>
<C>c</C>
</xql_result>
<xql_result>
<C>c</C>
</xql_result>
Other uses of the XQL package
Sybase doesn’t support these uses of the XQL package. These require JDK 1.2 or higher:
- · You can query XML documents from the command line, using the standalone application com.sybase.xml.xql.XqlDriver.
- · You can use Java package methods provided in com.sybase.xml.xql.Xql to query XML documents in Java applications. You can also use these Java package methods to query XML documents in Adaptive Server 12.5, using the Java VM feature.
- · The standalone app com.sybase.xml.xql.XqlDriver can parse and query only XML documents stored as files on your local system. You can’t use com.sybase.xml.xql.XqlDriver to parse or query XML documents stored in a database or over the network.
In addition, com.sybase.xml.xql.XqlDriver is useful for developing XQL scripts and learning XQL. However, Sybase recommends that you use com.sybase.xml.xql.XqlDriver only as a standalone program, not as part of another Java application, because com.sybase.xml.xql.XqlDriver includes a main() method. A Java program can only include one main() method, and if you include com.sybase.xml.xql.XqlDriver in another Java program that includes main(), the application attempts to implement both main() methods, which causes an error in Java.
Using XQL to develop standalone apps and more
In our next article, I’ll show some examples of using XQL to develop standalone applications, JDBC clients, JavaBeans, and Enterprise JavaBeans (EJBs) to access XML data. I’ll also show how to use the standalone XQL engine (com.sybase.xml.xql.XqlDriver) to validate an XQL document against its DTD.