6.6 Web Access to the Database

  Previous section   Next section

XSQL is a Java servlet for visualizing SQL query results. XSQL combines the power of SQL, XML, and XSLT to publish dynamic Web content based on database information. The servlet is similar to JSP (Java Server Pages) and follows the same mechanisms. Moreover, XSQL is interoperable with JSP. XSQL can be integrated in many popular Web servers like Apache 1.3.9 and higher, Oracle9iAS, or WebLogic 5.1. Thanks to Java, the servlet is easily extensible to one's needs.

6.6.1 The Principle of XSQL

The overall principle of XSQL is similar to JSP: A GET request with a URL of the form http://mycompany.com/myQuery.xsql references a file myQuery.xsql with a specific extension .xsql, denoting an XSQL servlet. The .xsql file mixes XML and SQL. The Web server recognizes the suffix .xsql, which has to be registered at the Web server before, and passes the file content to the XSQL servlet. The servlet processes the file and executes all the embedded SQL statements in the sense of server-side templates. The result, an XML document, is passed back to the caller.

In the easiest case, the XSQL file myQuery.xsql could have the content shown in Listing 6.29.

Listing 6.29 XSQL Example
<?xml version="1.0"?>
<xsql:query connection="demo" xmlns:xsql="urn:oracle-xsql">
  SELECT  . . .  FROM  . . .  WHERE  . . .
</xsql:query>

Principally, the file is an ordinary XML document. Queries are embedded by the special tag <xsql:query . . . >, the result of which is inserted at this place instead. xsql is the nickname for the namespace "urn:oracle-xsql" that defines all the XSQL tags.

The attribute connection="demo" fixes a database connection to be used for the query. The name "demo" refers to an entry in an XML configuration file named XSQLConfig.xml, which contains the details about the JDBC connection, as shown in Listing 6.30.

Listing 6.30 JDBC Connection
<connectiondefs>
   <connection name="demo">
      <username>scott</username>
      <password>tiger</password>
      <dburl> jdbc:oracle:thin:@myhost:1521:mydb </dburl>
      <driver> oracle.jdbc.driver.OracleDriver </driver>
      <autocommit> true </autocommit>
   </connection>
   <connection name="lite">
      <username>system</username>
      <password>manager</password>
      <dburl> jdbc:Polite:POlite </dburl>
      <driver> oracle.lite.poljdbc.POLJDBCDriver </driver>
      <autocommit> true </autocommit>
   </connection>
</connectiondefs>

The entry "demo" for <connection name> specifies a JDBC connection to the schema scott/tiger, using the database URL (<dburl>) (i.e., the database mydb on myhost using Oracle's "thin" JDBC driver); the database server is listening on port 1521 for incoming JDBC requests. Several connections can be specified that way. Here, a second connection "lite" is defined.

The result of <xsql:query> is processed in the manner of XSU. That is, the result is embedded in <ROWSET>, whereby each tuple becomes a <ROW> element. Again, object-relational concepts such as object-, reference-, and collection-valued attributes can be used in order to give the document a nested structure, according to the canonical XSU mapping. Using aliases helps to rename attributes to tags and to avoid invalid XML names.

The file content is an arbitrary XML document following the XML rules. It can embed xsql queries in any number, combine different query results, nest them, and so on (see Listing 6.31).

Listing 6.31 XSQL Query Examples
<Tag1>
   <xsql:query  . . . > Query1 </xsql:query>
   <Tag2>
      <xsql:query  . . . > Query2 </xsql:query>
   </Tag2>
   <xsql:query  . . . > Query3 </xsql:query>
</Tag1>

Several queries can be executed and aggregated to a valid XML document that way.

Being a valid XML document, an XSLT style sheet can be registered for the .xsql file, thus further processing the resulting document:

<?xml-stylesheet type="text/xsl" href="myXsl.xsl?>

myXsl.xsl is the style sheet that is applied to the resulting document. Integrating XSLT has several well-known advantages:

  • Style sheets are useful if the browser is unable to display XML. Then XSLT can convert XML into HTML, for instance.

  • Moreover, it is possible to present the same information in different ways, tailored to the capabilities of a specific client (PDA, browser, etc.). Common formats are HTML, Tiny HTML, HDML (Handheld Device Markup Language) for hand-held devices, WML (Wireless Markup Language) for wireless devices, and SVG (Scalable Vector Graphics).

The <xsql:query> element possesses various attributes such as fetch-size, skip-rows, max-rows, id-attribute (renaming the NUM-attribute), row-element (<ROW> tag), rowset-element (<ROWSET> tag), id-attribute-column (determining the column for setting the NUM attribute), and tag-case, which all can be used to control the naming of tags.

By default, an empty query result returns an XML document <ROWSET/>. However, putting <xsql:no-rows-query> SELECT . . . </xsql:no-rows-query> within an <xsql:query . . . > element, a query is registered as a substitute: If the original query yields an empty result, then <xsql:no-rows-query> is executed instead.

6.6.2 Posting XML Data into the Database

The XSQL approach also allows for storing XML documents in a fine-grained manner according to inverse canonical mapping. That is, the documents are "posted" into the database. The target is a table or a view. The principle is analogous to XSU; however, the table/view, the key, and the columns to be modified are specified by xsql attributes. The same holds for the batch size (commit-batch-size) and the registration of XSLT style sheets (transform). The XSQL tags are <xsql:insert-request>, <xsql:update-request>, and <xsql:delete-request> to insert, update, and delete documents, respectively (see Listing 6.32).

Listing 6.32 XSQL Update Example
<xsql:update-request table="MyCustomer" columns="Name"
key-columns="CNo">
   . . .  XML document  . . .
</xsql:update-request>

As an alternative, XSQL also can directly execute DML (Data Manipulation Language) statements using <xsql:dml> . . . </xsql:dml>. Consequently, data can be inserted, modified, and deleted in the database in an SQL-like manner via the Web by means of XSQL. The attribute commit="yes" sets an autocommit mode:

<xsql:dml commit="yes"> DELETE FROM MyCustomer WHERE . . .
<xsql:dml>

Analogously, an anonymous PL/SQL block can be executed in the database server.

6.6.3 Parameterization

Queries can be parameterized in a simple way. First of all, parameter names must be declared by using the bind-params attribute of <xsql:query> (see Listing 6.33).

Listing 6.33 XSQL bind-params Example
<?xml version="1.0"?>
<xsql:query connection="demo" bind-params="x y"
xmlns:xsql="urn:oracle-xsql">
  SQL-query
</xsql:query>

The value of the bind-params attribute is a space-separated list of parameter names whose left-to-right order indicates the positional bind variable to which its value will be bound in the statement. Here, two parameters x and y are declared. Hence, the SQL query possesses two parameters, which are denoted as "?". The first occurrence of "?" corresponds to the first parameter (i.e., x), and the second one to y. A GET request specifies values for the parameters, thus replacing "?" in the query:

http://mycompany.com/myQuery.xsql?x=1&y=2

Furthermore, parameters can be initialized with default values. In Listing 6.34, the parameter values are x=10 and y=20, unless the GET request provides different values.

Listing 6.34 XSQL Default Values for bind-params Example
<?xml version="1.0"?>
<example x="10" y="20">
 <xsql:query connection="demo" bind-params="x y"
 xmlns:xsql="urn:oracle-xsql">
    SELECT . . .
 </xsql:query>
</example>

The full power of XML is available in XSQL. For example, for any XSQL action element we can substitute the value of any attribute or the text of any contained SQL statement, by using a lexical substitution parameter. This allows one to dynamically set style sheets as href={ @name} .xsl, to parameterize configuration files as connection="{ @name}", or to control the result size or commit behavior by max-rows="{ @name} ". Listing 6.35 shows the principle.

Listing 6.35 XSQL max-rows Example
<example max="10" connection="demo" xlmns:xsql="urn:oracle-xsql">
   <xsql:query max-rows="{@max}"> SELECT . . . </xsql:query>
</example>

max="10" defines a default value for the whole <example> element. The attribute can be referenced by { @max}. The value of max is here used to restrict the query result to a certain number of tuples by means of max-rows. If the xsql-file is invoked without any further parameters, then max=10 is taken; otherwise, the default value is overridden by the actual parameter. Those substitutions { @var} can also be used to replace text in SQL queries: ORDER BY { @var}.

<xsql:include-request-params/> and <xsql:include-request-param name="P"/> are special forms to create an XML representation for all parameters or a certain parameter P, respectively. <xsql:include-request-params/> generates something like that shown in Listing 6.36.

Listing 6.36 XSQL include-request-params Example
<request>
   <parameters>
      <Param1> value </Param1>
      <Param2> value </Param2>
       . . .
   </parameters>
   <session>
      <SessionVariable1> value <SessionVariable1>
       . . .
   </session>
   <cookies>
      <CookieName1> value <CookieName1>
       . . .
   </cookies>
</request>

Param1 and Param2 are parameter names. <xsql:include-request-param name="P"/> returns the value of only one parameter P as value </P>. This is useful to give XSLT style sheets an opportunity to access the parameters?for example, to control XSL processing in a more powerful way. Even session variables and cookies are displayed by tags <session> and <cookies>, respectively. Additional advanced features assign constants or results of queries to session variable and cookies. Other concepts are known from JSP. <xsql:include-xml href="url"/> includes the XML contents of a local or remote resource referenced by the URL: The URL can be absolute, HTTP-based to retrieve XML from another Web site, or relative to include XML from a file on the file system, thus interpreting the URL relative to the XSQL page being processed. <xsql:include-xsql href="url"/> includes the results of one XSQL page into another page. This allows one to easily aggregate content from a page that has already been built.

6.5.9 Servlet Invocations

Besides a GET request via HTTP, .xsql files can be executed in the following ways:

  • Invoking a Java program from the command line:

    java oracle.xml.xsql.XSQLCommandLine myQuery.xsql output-file parameter5value . . .
    

    The Java program helps in learning the XSQL servlet by experience. Furthermore, the program is useful in generating XML for static data in order to store it in the file system. Successive accesses refer to the pregenerated data to avoid unnecessary generations. An output file is passed to the command, just as additional parameter values are. Using the special parameter xml-stylesheet, a URL to a style sheet can be registered. The parameter posted-xml provides a URL of an XML resource to be treated as if it were posted as part of the request. user-agent can be used to simulate a particular HTTP User-Agent string so that an appropriate style sheet for that User-Agent type will be selected when processing the page.

  • A shell command behaves similarly:

    xsql myQuery.xsql output-file parameter=value . . .
    
  • Java programs can use an oracle.xml.xsql.XSQLRequest object in the manner demonstrated in Listing 6.37.

  • Using the JSP mechanisms <jsp:include> and <jsp:forward>, XSQL files become part of a JSP application. This lets XSQL and JSP cooperate.

Listing 6.37 XSQLRequest Example
URL url = new URL ("myQuery.xsql");
XSQLRequest req = new XSQLRequest(url);
Hashtable params = new Hashtable(2);
params.put("x", "value1");
params.put("y", "value2");
req.process(params,new PrintWriter(System.out),
                   new PrintWriter(System.err));

The first three types of invocations do not require a servlet installation in the Web server.


Top

Part IV: Applications of XML