16.10.2015 Views

Getting Startedwith pureQuery

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

288<br />

<strong>Getting</strong> Started with <strong>pureQuery</strong><br />

create a query that exposes the XML data in relational format and XQuery transform<br />

expressions are used to persist updates back into the XML data stored in the database.<br />

Figure 15.1 - Giving control to the SQL layer<br />

Figure 15.1 shows a high level view of this approach. When reading data from the<br />

database, an SQL/XML statement containing an XMLTABLE() function is used to expose<br />

the XML data in relational format. The relational data rows are then interpreted by the<br />

<strong>pureQuery</strong> API, which uses them to populate the Java beans in the application layer. The<br />

SQL/XML statement used to do this mapping can be seen in Listing 16.1:<br />

public static void updateEmployee(Data data, Employee emp){<br />

String sql = "update employee set doc = xmlquery(" +<br />

"'copy $new := $DOC " +<br />

"modify (do replace value of $new/employee/name/first with<br />

$firstname," +<br />

"do replace value of $new/employee/name/last with $lastname," +<br />

"do replace value of $new/employee/office with $office)" +<br />

"return $new' " +<br />

"passing cast(:firstname as varchar(20)) as \"firstname\", " +<br />

"cast(:lastname as varchar(20)) as \"lastname\"," +<br />

"cast(:office as INTEGER) as \"office\"" +<br />

") where id = :id";<br />

data.update(sql, emp);<br />

}<br />

Listing 15.1 – SQL/XML Mapping<br />

You can use SQL/XML statements in either style of <strong>pureQuery</strong> programming: inline style or<br />

annotated method style. Please refer to Chapters 5 and 6 to learn more about these two<br />

different styles of programming with the <strong>pureQuery</strong> API.<br />

When persisting changes into the database, the <strong>pureQuery</strong> API fetches the updated values<br />

from the Java beans and passes them into the database, where an XQuery Transform

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!