Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 71d40963b505df4524269198e237b3e3 > files > 908

virtuoso-opensource-doc-6.1.4-2.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
 <head profile="http://internetalchemy.org/2003/02/profile">
  <link rel="foaf" type="application/rdf+xml" title="FOAF" href="http://www.openlinksw.com/dataspace/uda/about.rdf" />
  <link rel="schema.dc" href="http://purl.org/dc/elements/1.1/" />
  <meta name="dc.title" content="13. XML Support" />
  <meta name="dc.subject" content="13. XML Support" />
  <meta name="dc.creator" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="dc.copyright" content="OpenLink Software, 1999 - 2009" />
  <link rel="top" href="index.html" title="OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="search" href="/doc/adv_search.vspx" title="Search OpenLink Virtuoso Universal Server: Documentation" />
  <link rel="parent" href="webandxml.html" title="Chapter Contents" />
  <link rel="prev" href="queryingxmldata.html" title="Querying Stored XML Data" />
  <link rel="next" href="xmltemplates.html" title="XML Templates" />
  <link rel="shortcut icon" href="../images/misc/favicon.ico" type="image/x-icon" />
  <link rel="stylesheet" type="text/css" href="doc.css" />
  <link rel="stylesheet" type="text/css" href="/doc/translation.css" />
  <title>13. XML Support</title>
  <meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
  <meta name="author" content="OpenLink Software Documentation Team ;&#10;" />
  <meta name="copyright" content="OpenLink Software, 1999 - 2009" />
  <meta name="keywords" content="" />
  <meta name="GENERATOR" content="OpenLink XSLT Team" />
 </head>
 <body>
  <div id="header">
    <a name="updategrams" />
    <img src="../images/misc/logo.jpg" alt="" />
    <h1>13. XML Support</h1>
  </div>
  <div id="navbartop">
   <div>
      <a class="link" href="webandxml.html">Chapter Contents</a> | <a class="link" href="queryingxmldata.html" title="Querying Stored XML Data">Prev</a> | <a class="link" href="xmltemplates.html" title="XML Templates">Next</a>
   </div>
  </div>
  <div id="currenttoc">
   <form method="post" action="/doc/adv_search.vspx">
    <div class="search">Keyword Search: <br />
        <input type="text" name="q" /> <input type="submit" name="go" value="Go" />
    </div>
   </form>
   <div>
      <a href="http://www.openlinksw.com/">www.openlinksw.com</a>
   </div>
   <div>
      <a href="http://docs.openlinksw.com/">docs.openlinksw.com</a>
   </div>
    <br />
   <div>
      <a href="index.html">Book Home</a>
   </div>
    <br />
   <div>
      <a href="contents.html">Contents</a>
   </div>
   <div>
      <a href="preface.html">Preface</a>
   </div>
    <br />
   <div class="selected">
      <a href="webandxml.html">XML Support</a>
   </div>
    <br />
   <div>
      <a href="forxmlforsql.html">Rendering SQL Queries as XML (FOR XML Clause)</a>
   </div>
   <div>
      <a href="composingxmlinsql.html">XML Composing Functions in SQL Statements (SQLX)</a>
   </div>
   <div>
      <a href="xmlservices.html">Virtuoso XML Services</a>
   </div>
   <div>
      <a href="queryingxmldata.html">Querying Stored XML Data</a>
   </div>
   <div class="selected">
      <a href="updategrams.html">Using UpdateGrams to Modify Data</a>
    <div>
        <a href="#updategrambasics" title="Updategrams Basics">Updategrams Basics</a>
        <a href="#elementsdesc" title="Elements Description">Elements Description</a>
        <a href="#determiningactions" title="Determining Actions">Determining Actions</a>
        <a href="#usinginparams" title="Using Input Parameters">Using Input Parameters</a>
        <a href="#examples" title="Examples">Examples</a>
    </div>
   </div>
   <div>
      <a href="xmltemplates.html">XML Templates</a>
   </div>
   <div>
      <a href="xmlschema.html">XML DTD and XML Schemas</a>
   </div>
   <div>
      <a href="xq.html">XQuery 1.0 Support</a>
   </div>
   <div>
      <a href="xslttrans.html">XSLT Transformation</a>
   </div>
   <div>
      <a href="xmltype.html">XMLType</a>
   </div>
   <div>
      <a href="xmldom.html">Changing XML entities in DOM style</a>
   </div>
    <br />
  </div>
  <div id="text">
<a name="updategrams" />
    <h2>13.5. Using UpdateGrams to Modify Data</h2>

<p>Updategrams allow database updates to be defined as XML.  This
is ultimately achieved by mapping the XML nodes against corresponding
database columns.  Updategrams can be used to replace existing data
access components in a middle tier.  A typical application will
include a middle tier consisting of business logic and data access
code.  The data access code will interact with the database using
disconnected recordsets and command objects calling stored procedures.
Most of the data access section of the middle tier can be replaced
with updategrams.</p>

<p>Most data access tiers (both middle tier code and stored
procedures) will deal individually with specific database tables or
groups of related tables.  This can inhibit performance and often
several round trips to the database are required to complete a
transaction.  Updategrams solve this problem by including all the data
in an XML document that is then mapped to database tables and columns.
The entire database update can then be accomplished at once.  This
update can include inserting, updating and deleting data.</p>

<p>The <span class="computeroutput">xmlsql_update()</span> function supports
XML-based insert, update, and delete operations performed on an
existing table in the database.</p>

<a href="fn_xmlsql_update.html">xmlsql_update()</a>


<a name="updategrambasics" />
    <h3>13.5.1. Updategrams Basics</h3>

	<p>
The general format of an updategram is:
</p>

	<div>
      <pre class="programlisting">
&lt;sql:sync xmlns:sql=&quot;xml-sql&quot;&gt;
 &lt;sql:before&gt;
    &lt;TABLENAME [sql:id=&quot;value&quot;] col=&quot;value&quot; col=&quot;value&quot;?../&gt;
 &lt;/sql:before&gt;
 &lt;sql:after&gt;
    &lt;TABLENAME [sql:id=&quot;value&quot;] [sql:at-identity=&quot;value&quot;]
      col=&quot;value&quot; col=&quot;value&quot;?../&gt;
 &lt;/sql:after&gt;
&lt;/sql:sync&gt;
</pre>
    </div>

<p>
or
</p>

<div>
      <pre class="programlisting">
&lt;sql:sync xmlns:sql=&quot;xml-sql&quot;&gt;
        &lt;sql:before&gt;
                &lt;TABLENAME [sql:id=&quot;value&quot;]&gt;
		   &lt;col&gt;&quot;value&quot;&lt;/col&gt;
		   &lt;col&gt;&quot;value&quot;&lt;/col&gt;
		   ...
		&lt;/TABLENAME&gt;
		...
        &lt;/sql:before&gt;
        &lt;sql:after&gt;
                &lt;TABLENAME [sql:id=&quot;value&quot;] [sql:at-identity=&quot;value&quot;]&gt;
		   &lt;col&gt;&quot;value&quot;&lt;/col&gt;
		   &lt;col&gt;&quot;value&quot;&lt;/col&gt;
		   ...
		&lt;/TABLENAME&gt;
		...
        &lt;/sql:after&gt;
&lt;/sql:sync&gt;
</pre>
    </div>
<br />

	
	<a name="elementsdesc" />
    <h3>13.5.2. Elements Description</h3>

	<p>
The <span class="computeroutput">&lt;sync&gt;</span> tag of the updategram
signifies the beginning of an operation(s) The rows specified in the
<span class="computeroutput">&lt;before&gt;</span> refer to existing records in the
database.  The rows specified in the
<span class="computeroutput">&lt;after&gt;</span> block refer to what the user
wants in the database.  <span class="computeroutput">&lt;TABLENAME.../&gt;</span>
identifies target table.
</p>
	<p>
The <span class="computeroutput">sql:at-identity</span> attribute stores the last
identity value added by the system (if possible).  This identity value
can then be used in subsequent operations.
</p>
	<p>
The <span class="computeroutput">sql:id</span> attribute is used to mark rows. This
forces an association between the record specified in the
<span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> block in the update gram.  When
there are multiple instances specified, it is recommended that
<span class="computeroutput">sql:id</span> attribute be used for all the instances.
</p>
	<p>
Each <span class="computeroutput">&lt;TABLENAME.../&gt;</span> refers to a single
table. Multiple <span class="computeroutput">&lt;TABLENAME.../&gt;</span> entries
are allowed in the same <span class="computeroutput">&lt;before&gt;</span> or
<span class="computeroutput">&lt;after&gt;</span> tags, or in both
<span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> tags; however, nesting is not
allowed.  The <span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> tags are optional. A missing tag
is the same as having a tag with no content.
</p>
	<br />

<a name="determiningactions" />
    <h3>13.5.3. Determining Actions</h3>
	<p>
If only the <span class="computeroutput">&lt;after&gt;</span> block is specified,
the rows specified in the <span class="computeroutput">&lt;after&gt;</span> block
are inserted in the table(s). If both the
<span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> blocks are specified, then rows
specified in the <span class="computeroutput">&lt;after&gt;</span> block for which
there are no corresponding rows in the
<span class="computeroutput">&lt;before&gt;</span> block are inserted in the
table(s).
</p>
	<p>
In an update operation, the rows specified in the
<span class="computeroutput">&lt;before&gt;</span> block refer to existing rows in
the database. The corresponding rows in the
<span class="computeroutput">&lt;after&gt;</span> block reflect what the user wants
in the database. A row update operation is performed if there is a row
in both the <span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> sections with the same set of
values for the attributes that uniquely identify a row in a
table. Rows specified in the <span class="computeroutput">&lt;before&gt;</span>
block must be valid in the database for the updategram to successfully
update the rows.
</p>
	<p>
In a delete operation, if only the
<span class="computeroutput">&lt;before&gt;</span> block is specified in the update
gram, the rows specified in the <span class="computeroutput">&lt;before&gt;</span>
block are deleted from the table(s). If both the
<span class="computeroutput">&lt;before&gt;</span> and
<span class="computeroutput">&lt;after&gt;</span> blocks are specified, the rows
for which there are no corresponding rows in the
<span class="computeroutput">&lt;after&gt;</span> block are deleted from the
table(s).
</p>
	<br />

<a name="usinginparams" />
    <h3>13.5.4. Using Input Parameters</h3>
	<p>
Parameters declarations should be described in the &lt;header&gt;
section of the updategram.  There should be one
<span class="computeroutput">&lt;param&gt;</span> row for each parameter.
</p>
	<p>
General syntax:
</p>
	<div>
      <pre class="programlisting">
&lt;sql:header xmlns:sql=&quot;xml-sql&quot;&gt;
  &lt;sql:param name=&quot;PARAM_NAME&quot; [default=&quot;DEFAULT_VALUE&quot;]/&gt;
  ...
&lt;/sql:header&gt;
</pre>
    </div>
	<p>
Where <span class="computeroutput">PARAM_NAME</span> is the name of the parameter
and <span class="computeroutput">DEFAULT_VALUE</span> is optional default of
parameter Parameters in updategram should have
<span class="computeroutput">$PARAM_NAME</span> instead of a value.  On processing,
Virtuoso replaces <span class="computeroutput">$PARAM_NAME</span> with the
corresponding value from the
<span class="computeroutput">&lt;input_parameters&gt;</span> given to the function
<span class="computeroutput">xmlsql_update()</span>.
</p>
<br />


<a name="examples" />
    <h3>13.5.5. Examples</h3>
<p>
Given the following tables:
</p>
<div>
      <pre class="programlisting">
CREATE TABLE Orders (
    OrderID int identity,
    CustomerID varchar(10),
    EmpID int,
    PRIMARY KEY (OrderID));

CREATE TABLE OrderDetails (
    OrderID int,
    ProductID int,
    Quantity int);
</pre>
    </div>

<p>
A. Update Gram to Insert a Record
</p>

<div>
      <pre class="programlisting">
xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;ROOT xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
&lt;sql:sync&gt;
  &lt;sql:after&gt;
    &lt;Orders CustomerID=&quot;TEST&quot; EmpID=&quot;99&quot;/&gt;
  &lt;/sql:after&gt;
&lt;/sql:sync&gt;
&lt;/ROOT&gt;&#39;)));
</pre>
    </div>

<p>
B. Updategram with an <span class="computeroutput">at-identity</span> Attribute
</p>

<div>
      <pre class="programlisting">
xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;ROOT xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
&lt;sql:sync&gt;
  &lt;sql:after&gt;
    &lt;Orders sql:at-identity=&quot;x&quot; CustomerID=&quot;VINET&quot; EmpID=&quot;10&quot;/&gt;
      &lt;OrderDetails OrderID=&quot;x&quot; ProductID=&quot;1&quot; Quantity=&quot;50&quot;/&gt;
      &lt;OrderDetails OrderID=&quot;x&quot; ProductID=&quot;2&quot; Quantity=&quot;20&quot;/&gt;
    &lt;Orders sql:at-identity=&quot;x&quot; CustomerID=&quot;HANAR&quot; EmpID=&quot;11&quot;/&gt;
      &lt;OrderDetails OrderID=&quot;x&quot; ProductID=&quot;1&quot; Quantity=&quot;30&quot;/&gt;
      &lt;OrderDetails OrderID=&quot;x&quot; ProductID=&quot;4&quot; Quantity=&quot;25&quot;/&gt;
  &lt;/sql:after&gt;
&lt;/sql:sync&gt;
&lt;/ROOT&gt;&#39;)));
</pre>
    </div>

<p>
C. Updategram to Delete a Record
</p>

<div>
      <pre class="programlisting">
xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;ROOT xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
&lt;sql:sync&gt;
  &lt;sql:before&gt;
    &lt;Orders CustomerID=&quot;HANAR&quot; EmpID=&quot;11&quot;/&gt;
  &lt;/sql:before&gt;
&lt;/sql:sync&gt;
&lt;/ROOT&gt;&#39;)));
</pre>
    </div>

<p>
D. Updategram to Update a Record
</p>

<div>
      <pre class="programlisting">
xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;ROOT xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
&lt;sql:sync&gt;
  &lt;sql:before&gt;
    &lt;Orders sql:id=&quot;1&quot; CustomerID=&quot;VINET&quot; EmpID=&quot;10&quot;/&gt;
  &lt;/sql:before&gt;
  &lt;sql:after&gt;
    &lt;Orders sql:id=&quot;1&quot; CustomerID=&quot;VINET_NEW&quot; EmpID=&quot;11&quot;/&gt;
  &lt;/sql:after&gt;
&lt;/sql:sync&gt;
&lt;/ROOT&gt;&#39;)));
</pre>
    </div>

<p>
E: Using a different syntax for updategrams - entities in place
of attributes - example D can be transformed to:
</p>

<div>
      <pre class="programlisting">
xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;ROOT xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
&lt;sql:sync&gt;
  &lt;sql:before&gt;
    &lt;Orders sql:id=&quot;1&quot;&gt;
      &lt;CustomerID&gt;VINET&lt;/CustomerID&gt;
      &lt;EmpID&gt;10&lt;/EmpID&gt;
    &lt;/Orders&gt;
  &lt;/sql:before&gt;
  &lt;sql:after&gt;
    &lt;Orders sql:id=&quot;1&quot;&gt;
      &lt;CustomerID&gt;VINET_NEW&lt;/CustomerID&gt;
      &lt;EmpID&gt;11&lt;/EmpID&gt;
    &lt;/Orders&gt;
  &lt;/sql:after&gt;
&lt;/sql:sync&gt;
&lt;/ROOT&gt;&#39;)));
</pre>
    </div>

<p>
Note that two syntaxes cannot be mixed in one document.
</p>
<p>
F: Using input parameters
</p>

<p>
Assume the following table:
</p>
<div>
      <pre class="programlisting">
CREATE TABLE Shippers(
  ShipperID INTEGER,
  CompanyName VARCHAR(40),
  Phone VARCHAR(24),
  PRIMARY KEY (ShipperID));

xmlsql_update (xml_tree_doc (xml_tree (
&#39;&lt;DocumentElement xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;&gt;
    &lt;sql:header&gt;
      &lt;sql:param name=&quot;ShipperID&quot; default=&quot;2&quot;/&gt;
      &lt;sql:param name=&quot;CompanyName&quot; default=&quot;United Package New&quot;/&gt;
      &lt;sql:param name=&quot;Phone&quot; default=&quot;(503) 555-3199 (new)&quot;/&gt;
    &lt;/sql:header&gt;
    &lt;sql:sync&gt;
        &lt;sql:before&gt;
        &lt;/sql:before&gt;
        &lt;sql:after&gt;
            &lt;Shippers sql:id=&quot;1&quot; ShipperID=&quot;\$ShipperID&quot;
	    CompanyName=&quot;\$CompanyName&quot; Phone=&quot;\$Phone&quot;/&gt;
        &lt;/sql:after&gt;
    &lt;/sql:sync&gt;
&lt;/DocumentElement&gt;&#39;)),
    vector (&#39;ShipperID&#39;,&#39;10&#39;,&#39;CompanyName&#39;,&#39;DHL&#39;,&#39;Phone&#39;,&#39;+359 32 144&#39;));
		-- &lt;- this is a array with input parameters
</pre>
    </div>
<p>
This will add one record to the Shippers table with the data in the array.
Note that the slash/dollar sign pair &#39;\$&#39; transforms to dollar sign &#39;$&#39; only
</p>
<br />
<table border="0" width="90%" id="navbarbottom">
    <tr>
        <td align="left" width="33%">
          <a href="queryingxmldata.html" title="Querying Stored XML Data">Previous</a>
          <br />Querying Stored XML Data</td>
     <td align="center" width="34%">
          <a href="webandxml.html">Chapter Contents</a>
     </td>
        <td align="right" width="33%">
          <a href="xmltemplates.html" title="XML Templates">Next</a>
          <br />XML Templates</td>
    </tr>
    </table>
  </div>
  <div id="footer">
    <div>Copyright© 1999 - 2009 OpenLink Software All rights reserved.</div>
   <div id="validation">
    <a href="http://validator.w3.org/check/referer">
        <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" />
    </a>
    <a href="http://jigsaw.w3.org/css-validator/">
        <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
    </a>
   </div>
  </div>
 </body>
</html>