• Aucun résultat trouvé

In interacting with the database, XSQL solves a couple of basic problems. It converts SQL results to XML, and it converts XML to SQL. You’ve seen the conversion of SQL results to XML already with the xsql:queryaction, and you’ll see the XML-to-SQL conversion later in this chapter. In both of these cases, data that is usually arranged in a table has to be represented in a tree format. Oracle solves this problem with a canonical schema. This schema is used by xsql:querywhen result sets are presented as XML and is required by the XSQL actions that insert XML documents into database tables as relational data.

The schema uses the following common format for queries and XML insertions, updates, and deletions. In all these cases, you use XSLT transformations to massage the data. For queries, you use an XSLT stylesheet to transform the data to a format that is appropriate. Most often, this means transforming the canonical representation into HTML. When pushing data to the database, you use a stylesheet to transform the data from its original format into the canonical form.

The canonical form can be used to represent all of the Oracle datatypes, including cursors, objects, and collections. But first, say that you have a simple table in the data-base, which includes the values in Table 5.4.

Table 5.4 Simple Database Table

COLUMN_NAME_1 COLUMN_NAME_2

Value1.1 Value1.2

Value2.1 Value2.2

When you select all of the rows from the table, the canonical representation will appear as follows.

<ROWSET>

<ROW id=”1”>

<COLUMN_NAME_1>Value1.1</COLUMN_NAME_1>

<COLUMN_NAME_2>Value1.2</COLUMN_NAME_2>

</ROW>

<ROW id=”2”>

<COLUMN_NAME_1>Value2.1</COLUMN_NAME_1>

<COLUMN_NAME_2>Value2.2</COLUMN_NAME_2>

</ROW>

</ROWSET>

If you add more rows, you will get additional row elements. If you add more columns, you will have additional elements inside each row. The general pattern is that each row is an element that contains each field in that row as child element. In this case, you can assume that the datatypes are strings. In fact, it doesn’t matter. Numbers will appear the same way without any signifying formatting. In the canonical form, every-thing is text. This is also true for dates, although you control how dates are presented and interpreted with the date formats described later in this chapter. As you learned before, you can change the rowset and row element names when using xsql:query.

But even when these are changed, you have not changed the overall structure of the result datagram.

N OT E It’s important to note that the canonical representation is in XML and must follow the rules of XML. This implies that the column names, which vary from query to query, are restricted. They must begin with either a letter or an underscore and can contain only alphanumeric characters along with

underscores, hyphens, and periods. This doesn’t mean that you have to rename all of the columns in your database. It’s easy to alias column names in SQL; this is explained in the xsql:query section of this chapter, as well as in Chapter 8.

If you’ve been around SQL for a while, you know that not all queries produce such a simple output. You can have cursors that create their own tree structures. In such a case, the form looks like the following example:

<ROWSET>

<ROW id=”1”>

78 Chapter 5

<REGULAR_COLUMN>Regular Value</REGULAR_COLUMN>

<YOUR_CURSOR_NAME>

<YOUR_CURSOR_NAME_ROW id=”1”>

<FIELD1>Value For Field #1 of the Cursor</FIELD1>

<FIELD2>Value For Field #2 of the Cursor</FIELD2>

</YOUR_CURSOR_NAME_ROW>

</YOUR_CURSOR_NAME>

</ROW>

</ROWSET>

As you can see, cursors work quite naturally with the tree structure of XML. The fol-lowing real-world example demonstrates this more clearly. Here is a SQL query that you can run as the scott/tigeruser that utilizes cursors. If you aren’t familiar with cursors, don’t worry—they are covered in Chapter 8.

select dname, cursor(select ename from emp b where b.deptno=a.deptno) as employees from dept a

The result of the first two rows of this canonical query is as follows:

<ROWSET>

</EMPLOYEES_ROW>

</EMPLOYEES>

</ROW>

</ROWSET>

Oracle objects and collections also appear as nested structures in the canonical rep-resentation. The following example shows a single row with an object and a collection:

<ROWSET>

Because most applications use dates at some point, it is very important to understand how XSQL works with dates. XSQL uses date formats that act as masks. A date format is a combination of different symbols that represent different parts of the date-time stamp. XSQL uses date formats both when retrieving information from the database and when putting information into it. When retrieving information, XSQL writes the date to output in accordance with the date format. When inputting information, it assumes that dates are in the format specified by the date format.

The characters that represent different parts of the date format are listed in Table 5.5.

Padding for variable-length data can be supplied by repeating the symbol. For exam-ple, if you want 08to represent 8 A.M., you put hhin the format string.

Table 5.5 Date Format Symbols

SYMBOL MEANING EXAMPLE

G Era designator AD

y Year 1996

MM Month in year as number 07

80 Chapter 5