DB2 Universal Database 8.x 的 XML 支持建立在關係基礎設施之上。在 Viper 之前,XML 數據只有兩條路:要麼原樣存成 CLOB(字符大對象),要麼拆解到關係表中。
DB2 UDB 9.1 則提供了對 XML 的真正原生支持。XML 成為一種新的數據類型,數據以解析後的帶注釋樹形式存儲,獨立於關係數據存儲;基於 XML 模式的 XML 索引被引入,同時新增了用於查詢與發布 XML 的 XQuery 和 SQL/XML 支持。
本文是「從 DB2 8.x 遷移到 Viper」系列的第一篇,聚焦一個很實際的缺口:DB2 8.x 中沒有任何開箱即用的子文檔更新能力。
缺少這個功能的原因並不難理解:XQuery 中關於更新的標準至今尚未定案。
一種繞法是:把文檔取到客戶端,改完再存回去。但這條路受客戶端環境的 XML 能力限制,還要求有熟悉 DOM 的專家來寫客戶端代碼。
更好的辦法是設計一個存儲過程,直接在數據庫內部完成更新,文檔不必出門。它支持對原生存儲的 XML 文檔做部分更新,具体包括:
更新值本身也有三種來源:靜態嵌入在調用中、用 SQL 在運行時動態生成、或用算術表達式基於初始文本或屬性值計算。
需要說明的是:在後台,存儲過程執行的仍是完整的文檔更新——部分更新只是接口層的說法。
DB2XMLFUNCTIONS.XMLUPDATE(commandXML, querySQL, updateSQL, errorCode, errorMsg)
參數逐一說明:
命令 XML 的結構如下:
<updates namespaces=""> <update using="" col="" action="" path="">update value</update> </updates>
各屬性含義:
@namespace:分號分隔的「前綴:名稱空間」字符串,前綴用於路徑表達式。只有在路徑中用到名稱空間時才需要,但即使默認名稱空間也必須用前綴限定。@col:必填。對應 querySQL 中要修改的列位置,從 1 開始計數。@path:必填。目標節點的 XPath 位置。路徑無效則存儲過程中止。名稱空間中不能使用通配符。@using:唯一有效值是 SQL。設為 SQL 時,update 元素的子節點被當作查詢,取結果第一行第一列作為新值;查詢失敗則中止。若要跑 XQuery,可用 Xquery 關鍵字或 SQL/XML 函數把 XQuery 嵌進 SQL。@action:可選,不設則默認為 replace。取值有 replace(替換目標節點)、append(作為子節點追加)、delete(刪除目標節點)、compute(把值當參數化表達式,其中問號由目標節點現有文本值替換,計算結果寫回;計算結果的路徑只能是葉節點)。update value:update 元素的子節點,可以是文本也可以是元素。action=delete 時不需要。當 @using="SQL" 或 @action="compute" 時應為文本值,其餘情況視為要替換的值。一個重要提醒:從命令行處理器(CLP)調用時,updateSQL 必須賦值。若設為 null 或空字符串,CLP 會拋出 JCC 異常「Column not updatable」。從 Java 應用代碼內部調用時也會遇到同樣情況。
先把 jar 裝進 DB2(只需一次),然後為每個數據庫分別註冊存儲過程。
如果希望在存儲過程中執行 XQuery,還需要為 DB2 配置 JCC 驅動。確認 DB2 運行中,從 DB2 命令窗口執行:
db2set DB2_USE_DB2JCCT2_JROUTINE=on
編譯並打包(Windows 環境):
SET CLASSPATH=.;%DB2PATH%\java\db2Java.zip; %DB2PATH%\java\db2jcc.jar;%DB2PATH%\java\db2jcc_license_cisuz.jar; "%DB2PATH%\java\jdk\bin\javac.exe" -d . *.java "%DB2PATH%\java\jdk\bin\jar" cvf db2xmlfunctions.jar com/ibm/db2/XML/functions/*.class
安裝 jar:
DB2 -t
connect to your_dbname
CALL SQLJ.INSTALL_JAR('file:/temp/samples/db2xmlfunctions.jar',
db2XMLfunctions, 0);
註冊存儲過程:
CREATE PROCEDURE db2xmlfunctions.XMLUPDATE( IN COMMANDSQL VARCHAR(32000), IN QUERYSQL VARCHAR(32000), IN UPDATESQL VARCHAR(32000), OUT errorCode INTEGER, OUT errorMsg VARCHAR(32000)) DYNAMIC RESULT SETS 0 LANGUAGE Java PARAMETER STYLE Java NO DBINFO FENCED NULL CALL MODIFIES SQL DATA PROGRAM TYPE SUB EXTERNAL NAME 'db2xmlfunctions:com.ibm.db2.xml.functions.XMLUpdate.Update';
改了代碼要重新註冊前,先卸載舊版本:
DROP PROCEDURE DB2XMLFUNCTIONS.XMLUPDATE( VARCHAR(32000), VARCHAR(32000), VARCHAR(32000), INTEGER, VARCHAR(32000)); CALL SQLJ.REMOVE_JAR(DB2XMLFUNCTIONS);
另外,如果遇到 java.lang.OutOfMemoryError,記得調大 Java 堆:
db2 update dbm cfg using Java_HEAP_SZ 1024
Create table XMLCustomer(cid integer not null PRIMARY KEY, info XML);
Insert into XMLCustomer (cid, info) values (1006,
XMLPARSE(DOCUMENT '
<customerinfo xmlns="https://posample.org" Cid="1006">
<name>Hardeep Singh</name>
<addr country="United States">
<street>555 Bailey Ave</street>
<city/>
<prov-state>CA</prov-state>
<pcode-zip>95141</pcode-zip>
</addr>
<phone type="">543-4610</phone>
</customerinfo>'
PRESERVE WHITESPACE));
由於更新會改動原文檔,做某些示例前需要刪除並重新插入這條記錄。
把簡單的 name 元素換成結構化的複合元素:
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update action="replace" col="1" path="/x:customerinfo/x:name">
<name><fname>Hardeep</fname><lname>Singh</lname></name>
</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=? where cid=1006', ?, ?);
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update using="sql" action="replace" col="1"
path="//x:customerinfo[@Cid=1006]/x:addr/x:pcode-zip/text()">
select cid from XMLCustomer where cid=1006
</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=? where cid=1006', ?, ?);
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update action="compute" col="1"
path="/x:customerinfo/x:addr/x:pcode-zip/text()">
(20+?)*32-?
</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=? where cid=1006', ?, ?);
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update using="sql" action="replace" col="1"
path="/x:customerinfo/x:addr/x:pcode-zip/text()">
select cid from XMLCustomer where cid=1006
</update>
<update action="compute" col="1"
path="/x:customerinfo/x:addr/x:pcode-zip/text()">
(2+?)*10-?
</update>
<update action="delete" col="1" path="/x:customerinfo/x:name"/>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=? where cid=1006', ?, ?);
先創建模式並在 XSR 中註冊,然後在 updateSQL 裡套一層 XMLvalidate:
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update using="sql" action="replace" col="1"
path="/x:customerinfo/x:addr/x:pcode-zip/text()">
select cid from XMLCustomer where cid=1006
</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=XMLvalidate(? according to XMLSCHEMA ID test.schema2)
where cid=1006', ?, ?);
-- 替換屬性 path="/x:customerinfo/x:phone/@type" -- 替換文本 path="/x:customerinfo/x:addr/x:city/text()"
這裡有一個必須注意的細節:路徑末尾一定要寫 text()。這樣即使目標元素是空元素(沒有現有文本節點),更新也能生效。省略 text() 又不存在現有文本值時,更新命令會直接失敗。
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update action="append" col="1" path="/x:customerinfo/x:addr">
<county>Santa Clara</county>
</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'update XMLCustomer set info=? where cid=1006', ?, ?);
注意:新節點不屬於任何名稱空間。
Call DB2XMLFUNCTIONS.XMLUPDATE(
'<updates namespaces="x:https://posample.org">
<update action="replace" col="1" path="/x:customerinfo/x:name">
<name>Marja Soininen</name>
</update>
<update action="replace" col="1" path="/x:customerinfo/@Cid">1008</update>
</updates>',
'Select info from XMLCustomer where cid=1006',
'insert into XMLCustomer (cid, info) values (1008, cast(? as XML))', ?, ?);
-- 顯式刪除 <update action="delete" col="1" path="/x:customerinfo/x:name"/> -- 不寫 action,默認執行替換 <update col="1" path="//x:customerinfo[@Cid=1006]/x:phone"> <phone><areacode>910</areacode></phone> </update>
名稱空間不匹配時(例如文檔用的是 posample.org,命令裡寫成 my.org),會返回錯誤碼 1,並給出類似消息:
<error type='abort' action='replace'
msg='Cannot find path //x:customerinfo[@Cid=1006]/x:phone) in the XMLDocument'>
遺漏 path 屬性時:
<error type='abort' action='null' msg='path not defined'></error>
替換節點時若希望新節點留在同一名稱空間中,需要在替換內容上顯式聲明:
<update action="replace" col="1" path="/x:customerinfo/x:name">
<name xmlns="https://posample.org">
<fname>Marja</fname><lname>Soininen</lname>
</name>
</update>
這個更新存儲過程填補了 DB2 8.x 中「無法原地更新 XML」的空白,讓遷移路徑上少了一個障礙。下一篇將深入探討新的原生 XML 支持對遷移現有 XML 應用的具體影響。
本文的完成要感謝 Matthias Nicola、Bert Van der Linden、Irina Kogan、Annie Wang、Ying Chen 和 Xiaoli Du 的協助。