麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫 > DB2 > 正文

將XML應(yīng)用程序從DB2 8.x遷移到Viper

2024-09-06 23:58:11
字體:
供稿:網(wǎng)友

簡介

db2 universal database (udb) version 8.x 中的 xml 支持基于 db2 的關(guān)系基礎(chǔ)設(shè)施。在 db2 viper 版本之前,xml 數(shù)據(jù)要么原樣存儲為字符大對象 (clob),要么被分解到關(guān)系表中。

相反,db2 udb version 9.1 具有對 xml 數(shù)據(jù)的真正本機支持。xml 現(xiàn)在被當(dāng)作新的數(shù)據(jù)類型,xml 數(shù)據(jù)存儲在經(jīng)過解析的帶注釋的樹中,獨立于關(guān)系數(shù)據(jù)存儲。基于 xml 模式的 xml 索引已經(jīng)引入,同時還引入了對用于查詢和發(fā)布 xml 數(shù)據(jù)的語言 xquery 和 sql/xml 的支持。為了理解這些新的 xml 特性對遷移的重大意義,需要將 db2 8.x 中用于存儲和查詢 xml 數(shù)據(jù)的不同技術(shù)與 db2 udb version 9 中可用的類似的或新的 xml 功能相比較。

本文是關(guān)于將 xml 應(yīng)用程序從 db2 8.x 遷移到 db2 viper 的三篇系列文章中的第一篇。該系列從描述一個基于 java 的存儲過程開始,您可以使用該存儲過程來對 xml 數(shù)據(jù)執(zhí)行子文檔更新。您可以下載更新后的存儲過程的源代碼和 jar 文件,并根據(jù)說明安裝它。

第二篇文章比較 db2 version 8.x 中和 db2 viper 中的 xml 特性。然后簡要討論 db2 viper 中引入的新 xml 特性,并詳細介紹新 xml 支持對于遷移現(xiàn)有基于 xml 的應(yīng)用程序的重大意義。這篇文章還包括基于 java 的實用工具的源代碼,該工具用于幫助生成遷移數(shù)據(jù)庫對象時所需的腳本。

本系列的最后一篇文章介紹分步示例遷移場景。它包括示例場景的源代碼。

更新存儲過程

對于本機存儲在 db2 中的 xml 文檔,不存在用于執(zhí)行子文檔更新的開箱即用的功能。缺少該功能的原因是,目前還沒有定義 xquery 中更新的標準。

該問題的一種解決方案是,將文檔交給客戶機,修改它,然后再將其保存到數(shù)據(jù)庫中。該方法受到客戶機環(huán)境的 xml 功能的限制,并且還要求專家級的人員編寫基于文檔對象模型 (dom) 的客戶機。

通過創(chuàng)建更新存儲過程,可以更新數(shù)據(jù)庫中的 xml 文檔而無需將其交給客戶機。該存儲過程支持對本機存儲在數(shù)據(jù)庫中的 xml 文檔進行部分更新。

存儲過程允許:

更改目標 xml 文檔中任何文本或?qū)傩怨?jié)點的值

使用另一個 xml 元素替換 xml 文檔中的元素節(jié)點(及其所有子節(jié)點)

刪除 xml 文檔中的節(jié)點

插入新元素

多次更新源文檔

更新多個源文檔

使用修改的 xml 文檔替換另一個 xml 文檔

將修改的文檔插入新記錄

更新信息可以:

靜態(tài)地嵌入更新調(diào)用中

使用 sql 在運行時動態(tài)地創(chuàng)建

使用算術(shù)表達式基于初始文本或?qū)傩灾颠M行計算

注意:在后臺,更新存儲過程仍然執(zhí)行完整的文檔更新。

 

 

xmlupdate 命令

db2xmlfunctions.xmlupdate (commandxml、querysql、updatesql、errorcode、errormsg)

commandxml —— 該參數(shù)是一個用于封裝更新命令的 xml 字符串。這些命令將應(yīng)用到由 querysql 所選擇的 xml 文檔。

該命令的結(jié)構(gòu)是:

<updates namespaces=""> <update using="" col="" action="" path="">update value</update> </updates>

—— 這是用于包裝所有更新命令元素的根元素。

@namespace —— 該屬性的值應(yīng)該是由分號分隔的 “前綴:名稱空間” 字符串。前綴然后用于在 xml 文檔中導(dǎo)航時使用的任何路徑表達式中。

essential —— 否(僅當(dāng)在任何路徑中使用名稱空間時才需要它)。

即使是默認名稱空間也必須用一個前綴進行限定。

—— 該元素定義需要在目標 xml 文檔上執(zhí)行的每個修改。

occurrence —— 可以定義這些元素中的一個或多個。

每次出現(xiàn)處理文檔的一個修改。

@col —— 該屬性的值應(yīng)該是對應(yīng)于要在 querysql 中修改的列位置的編號。

essential —— 是。

valid value —— 列位置從 1 開始。

@path —— 該屬性的值是目標 xml 文檔中節(jié)點的 xpath 位置。如果路徑無效,則存儲過程將中止。

essential —— 是。

valid value —— xpath 表達式。

如果要在 xpath 中使用名稱空間,請確保設(shè)置名稱空間屬性。

不能將通配符用于名稱空間。

@using —— 該屬性的惟一有效值是 sql。如果該屬性存在并設(shè)置為 sql,那么 update value( 元素的子節(jié)點)被視為一個 sql 查詢。查詢結(jié)果的第一行中的第一列將用作新的 update value。如果查詢失敗,那么存儲過程將中止。

essential —— 否。

valid value —— sql。

對于 xquery,可以使用關(guān)鍵字 xquery,也可以使用 sql/xml 函數(shù)將 xquery 嵌入 sql。

@action —— 該屬性定義將在 xml 文檔中的目標節(jié)點(使用 @path 屬性中定義的 xpath 來定位)上進行的操作。如果操作失敗,那么存儲過程將中止。

essential —— 否。如果未設(shè)置操作,則假定是一個替換操作。

valid value —— 替換、追加、刪除和計算:

replace —— 使用 update value 替換目標節(jié)點。

append —— 將 update value 作為子節(jié)點追加到目標節(jié)點。

delete —— 刪除目標節(jié)點。

compute —— 將 update value 當(dāng)作參數(shù)化的表達式。表達式中的問號 (?) 將由目標節(jié)點的現(xiàn)有文本值替換。然后,計算表達式,得到的值將替換目標節(jié)點中的現(xiàn)有值。計算出的值的 xpath 只能是葉節(jié)點。

update value —— 這為每個更新命令 (//update/*) 的子節(jié)點。它可以是文本節(jié)點,也可以是元素。

essential —— 否。對于 action=delete,不需要它。

valid value —— 當(dāng) @using 屬性被設(shè)置為 sql 時,子節(jié)點應(yīng)該是文本值。它被當(dāng)作 sql 表達式。當(dāng) @action 屬性被設(shè)置為 compute 時,子節(jié)點應(yīng)該是文本值。它被當(dāng)作參數(shù)化表達式。在所有其他情況下,子節(jié)點都視為要替換的值。

querysql —— 任何用于檢索需要更新的 xml 文檔的有效 sql 選擇語句。

essential —— 是。

valid value —— 只能選擇 xml 文檔。如果選擇了其他列,存儲過程就會中止。

updatesql —— 它表示參數(shù)化的更新 sql。修改的 xml 文檔作為運行時參數(shù)被綁定到更新 sql。它允許將修改的 xml 文檔保存到數(shù)據(jù)庫中的其他 xml 列中。

essential —— 否。如果該參數(shù)為 null,則使用可更新的游標來修改所選的列。

重要事項:從命令行處理器 (clp) 執(zhí)行更新存儲過程時,始終需要設(shè)置 updatesql 參數(shù)的值。如果將這個值設(shè)置為 null 或空字符串,clp 會拋出 jcc 異常:column not updatable。當(dāng)您從應(yīng)用程序代碼 (java) 內(nèi)部調(diào)用更新存儲過程時,將發(fā)生此情況(即 updatesql 設(shè)置為 null)。

errorcode —— 值為 -1 指示存儲過程因發(fā)生某種錯誤而中止。如果更新成功,則返回指示已更新的記錄數(shù)的正值。

errormsg —— 錯誤消息,包括 xml 解析器和 jcc 驅(qū)動程序拋出的任何異常。

注意:如果得到 java.lang.outofmemoryerror,應(yīng)該增加 java 堆大小:

db2 update dbm cfg using java_heap_sz 1024

 

設(shè)置存儲過程

首先需要將更新存儲過程 jar 安裝到 db2 中。這個過程僅需執(zhí)行一次。下一步,對于每個數(shù)據(jù)庫,需要分別注冊存儲過程。

重要事項:如果希望在存儲過程中執(zhí)行 xqueries,那么需要為 db2 設(shè)置 jcc 驅(qū)動程序。確保 db2 在運行,然后從 db2 命令窗口執(zhí)行以下命令:

db2set db2_use_db2jcct2_jroutine=on

設(shè)置更新存儲過程的步驟

通過執(zhí)行以下步驟編譯 java 代碼并創(chuàng)建 db2xmlfunctions.jar 文件。

注意:db2xmlfunctions.jar 還可以從 下載 部分下載。如果您選擇下載該文件,則跳過 第 2 步。

創(chuàng)建目錄 /temp/samples。

將 xmlupdate_code.zip(可以在 下載 部分找到)復(fù)制到 temp 目錄。

將 xmlupdate.java 和 xmlparse.java 文件解壓到 /temp/samples 目錄。

編譯 java 文件并為 udf 創(chuàng)建 jar 文件。

在 microsoft windows 上,打開 db2 命令窗口:

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

在 aix 上,將 db2path 設(shè)置為 db2 sqllib 目錄:

classpath=$db2path/java/sqlj.zip:$db2path/java/db2java.zip $db2path/java/jdk/bin/javac.exe" -d . *.java $db2path/java/jdk/bin/jar" cvf db2xmlfunctions.jar com/ibm/db2/xml/functions/*.class

注意:上述命令假定使用 sh 或 bash shell。根據(jù)需要更改為 csh、tsh 等。

在 db2 中安裝存儲過程:

db2 -t connect to your_dbname&yuml; call sqlj.install_jar('file:/temp/samples/db2xmlfunctions.jar' , db2xmlfunctions,0);

在數(shù)據(jù)庫中注冊存儲過程:

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' ; terminate;

刪除存儲過程

如果更改了存儲過程,那么在注冊新版本之前應(yīng)該首先從 db2 卸載它:

drop procedure db2xmlfunctions.xmlupdate(varchar(32000), varchar(32000),varchar(32000),integer, varchar(32000)); call sqlj.remove_jar(db2xmlfunctions);

 

 

xmlupdate 示例

對于 xmlupdate 示例,請執(zhí)行以下步驟:

創(chuàng)建測試表:

create table xmlcustomer(cid integer not null primary key, info xml );

將示例 xml 文檔插入表中:

insert into xmlcustomer (cid, info ) values (1006 , xmlparse ( document ' <customerinfo xmlns=" http://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 ) );

注意:由于更新調(diào)用修改了初始的 xml 文檔,所以您需要為某些查詢而刪除插入的文檔,并重新插入它。

示例查詢

下面是示例查詢:

替換節(jié)點:action=replace。

通過使用復(fù)雜名稱元素替換簡單名稱元素來更新測試文檔:

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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',?,?);

使用 sql 查詢獲取新值以進行更新:

using=sql。 call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <update using="sql" action="replace" col="1" path="http://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',?,?);

使用給定表達式來計算值:

action=compute。 call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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=? &yuml;here cid=1006',?,?);

對目標 xml 文檔執(zhí)行多個操作:

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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',?,?);

更新文檔時對其進行驗證。

為此,您需要創(chuàng)建模式并在 xsr 中注冊。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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',?,?)

使用 xmlupdate 替換屬性值。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:phone/@type"> tie line </update> </updates>', 'select info from xmlcustomer where cid=1006', 'update xmlcustomer set info=? where cid=1006',?,?);

使用 xmlupdate 替換文本值。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:addr/x:city/text()"> san jose </update> </updates>', 'select info from xmlcustomer where cid=1006', 'update xmlcustomer set info=? where cid=1006',?,?);

 

 

重要事項:必須在路徑的末尾指定 text()。這一步確保即使是空元素(即不具有現(xiàn)有文本節(jié)點的元素)也進行更新。如果省略了 text() 且不存在要替換的現(xiàn)有文本值,更新命令就會失敗。

使用 xmlupdate 追加子節(jié)點。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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',?,?);

注意:新節(jié)點 不在任何名稱空間中。

使用 xmlupdate 將更新的 xml 插入新行。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://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))',?,?);

使用 xmlupdate 刪除節(jié)點。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <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',?,?);

當(dāng)更新元素中沒有設(shè)置 @action 時,就默認執(zhí)行替換操作。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <update col="1" path="http://x:customerinfo[@cid=1006]/x:phone"> <phone><areacode>910</areacode></phone> </update> </updates>', 'select info from xmlcustomer where cid=1006', 'update xmlcustomer set info=? where cid=1006',?,?);

以下示例展示帶有無效名稱空間或帶有沒有前綴的名稱空間的 xmlupdate:

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://my.org"> <update col="1" path="http://x:customerinfo[@cid=1006]/x:phone"> <phone><areacode>910</areacode></phone> </update> </updates>', 'select info from xmlcustomer where cid=1006', 'update xmlcustomer set info=? where cid=1006',?,?);

該查詢返回設(shè)置為 1 的錯誤,以及如下錯誤消息:

<error type='abort' action='replace' msg='cannot find path //x:customerinfo[@cid=1006]/x:phone) in the xmldocument'>

以下示例展示的 xmlupdate 在更新元素中有一個遺漏的路徑:

call db2xmlfunctions.xmlupdate ( '<updates > <update col="1"> (20+?)*32-? </update></updates>', 'select info from xmlcustomer where cid=1006', 'update xmlcustomer set info=? where cid=1006',?,?);

該查詢返回設(shè)置為 1 的錯誤,以及如下錯誤消息:

<error type='abort' action='null' msg='path not defined'></error>

使用相同名稱空間中的新節(jié)點替換某個節(jié)點。

call db2xmlfunctions.xmlupdate ( '<updates namespaces="x:http://posample.org"> <update action="replace" col="1" path="/x:customerinfo/x:name"> <name xmlns="http://posample.org"> <fname>marja</fname><lname>soininen</lname> </name> </update> </updates>', 'select info from xmlcustomer where cid=1008', 'insert into xmlcustomer (cid, info ) values (1007, cast( ? as xml))',?,?);

結(jié)束語

本文描述的更新存儲過程允許對本機存儲在數(shù)據(jù)庫中的 xml 文檔進行部分更新。下一篇文章將深度挖掘和具體研究新的 xml 支持對遷移現(xiàn)有基于 xml 的應(yīng)用程序的重大意義。

致謝

感謝 matthias nicola、bert van der linden、irina kogan、annie wang、ying chen 和 xiaoli du 在撰寫這篇文章時給予的幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 一级成人欧美一区在线观看 | 失禁高潮抽搐喷水h | 成人午夜网址 | 黑色丝袜美美女被躁视频 | 国产精品一区二区三区99 | 日本aaa一级片 | 日本精品视频一区二区三区四区 | xxxx18韩国护士hd老师 | 超久久| 久久免费视频精品 | 久久久久夜色精品国产老牛91 | 最近中文字幕一区二区 | 一区二区视频在线看 | 久久亚洲精品国产一区 | www.com超碰 | 97青青草视频 | 久久一本日日摸夜夜添 | 欧美日韩高清一区二区三区 | 久久久久成人免费 | 视频一区二区中文字幕 | 天天天干夜夜夜操 | 成人黄色小视频在线观看 | 久久精品成人影院 | 亚洲人成中文字幕在线观看 | 看国产一级毛片 | 日本aⅴ在线 | 久久男 | 黄色成人在线 | 一区二区三区在线视频观看58 | 欧美成人一区二区三区 | 亚洲精品av在线 | 国产亚洲欧美在线视频 | 欧美一级片免费在线观看 | 99精品视频在线免费观看 | 一级大片久久 | 深夜精品福利 | 黑人三级毛片 | 国产精品久久久免费看 | 极色品影院 | 欧美一区二区精品夜夜嗨 | 日韩视频―中文字幕 |