首页 > 数据库 > DB2 > 正文

将XML应用程序从DB2 8.x迁移到Viper

2020-03-09 21:24:01
字体:
来源:转载
供稿:网友

简介

db2 universal database (udb) version 8.x 中的 xml 支持基于 db2 的关系基础设施。在 db2 viper 版本之前,xml 数据要么原样存储为字符大对象 (clob),要么被分解到关系表中。

相反,db2 udb version 9.1 具有对 xml 数据的真正本机支持。xml 现在被当作新的数据类型,xml 数据存储在经过解析的带注释的树中,独立于关系数据存储。基于 xml 模式的 xml 索引已经引入,同时还引入了对用于查询和发布 xml 数据的语言 xquery 和 sql/xml 的支持。为了理解这些新的 xml 特性对迁移的重大意义,需要将 db2 8.x 中用于存储和查询 xml 数据的不同技术与 db2 udb version 9 中可用的类似的或新的 xml 功能相比较。

本文是关于将 xml 应用程序从 db2 8.x 迁移到 db2 viper 的三篇系列文章中的第一篇。该系列从描述一个基于 java 的存储过程开始,您可以使用该存储过程来对 xml 数据执行子文档更新。您可以下载更新后的存储过程的源代码和 jar 文件,并根据说明安装它。

第二篇文章比较 db2 version 8.x 中和 db2 viper 中的 xml 特性。然后简要讨论 db2 viper 中引入的新 xml 特性,并详细介绍新 xml 支持对于迁移现有基于 xml 的应用程序的重大意义。这篇文章还包括基于 java 的实用工具的源代码,该工具用于帮助生成迁移数据库对象时所需的脚本。

本系列的最后一篇文章介绍分步示例迁移场景。它包括示例场景的源代码。

更新存储过程

对于本机存储在 db2 中的 xml 文档,不存在用于执行子文档更新的开箱即用的功能。缺少该功能的原因是,目前还没有定义 xquery 中更新的标准。

该问题的一种解决方案是,将文档交给客户机,修改它,然后再将其保存到数据库中。该方法受到客户机环境的 xml 功能的限制,并且还要求专家级的人员编写基于文档对象模型 (dom) 的客户机。

通过创建更新存储过程,可以更新数据库中的 xml 文档而无需将其交给客户机。该存储过程支持对本机存储在数据库中的 xml 文档进行部分更新。

存储过程允许:

更改目标 xml 文档中任何文本或属性节点的值

使用另一个 xml 元素替换 xml 文档中的元素节点(及其所有子节点)

删除 xml 文档中的节点

插入新元素

多次更新源文档

更新多个源文档

使用修改的 xml 文档替换另一个 xml 文档

将修改的文档插入新记录

更新信息可以:

静态地嵌入更新调用中

使用 sql 在运行时动态地创建

使用算术表达式基于初始文本或属性值进行计算

注意:在后台,更新存储过程仍然执行完整的文档更新。

 

 

xmlupdate 命令

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

commandxml —— 该参数是一个用于封装更新命令的 xml 字符串。这些命令将应用到由 querysql 所选择的 xml 文档。

该命令的结构是:

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

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

@namespace —— 该属性的值应该是由分号分隔的 “前缀:名称空间” 字符串。前缀然后用于在 xml 文档中导航时使用的任何路径表达式中。

essential —— 否(仅当在任何路径中使用名称空间时才需要它)。

即使是默认名称空间也必须用一个前缀进行限定。

—— 该元素定义需要在目标 xml 文档上执行的每个修改。

occurrence —— 可以定义这些元素中的一个或多个。

每次出现处理文档的一个修改。

@col —— 该属性的值应该是对应于要在 querysql 中修改的列位置的编号。

essential —— 是。

valid value —— 列位置从 1 开始。

@path —— 该属性的值是目标 xml 文档中节点的 xpath 位置。如果路径无效,则存储过程将中止。

essential —— 是。

valid value —— xpath 表达式。

如果要在 xpath 中使用名称空间,请确保设置名称空间属性。

不能将通配符用于名称空间。

@using —— 该属性的惟一有效值是 sql。如果该属性存在并设置为 sql,那么 update value( 元素的子节点)被视为一个 sql 查询。查询结果的第一行中的第一列将用作新的 update value。如果查询失败,那么存储过程将中止。

essential —— 否。

valid value —— sql。

对于 xquery,可以使用关键字 xquery,也可以使用 sql/xml 函数将 xquery 嵌入 sql。

@action —— 该属性定义将在 xml 文档中的目标节点(使用 @path 属性中定义的 xpath 来定位)上进行的操作。如果操作失败,那么存储过程将中止。

essential —— 否。如果未设置操作,则假定是一个替换操作。

valid value —— 替换、追加、删除和计算:

replace —— 使用 update value 替换目标节点。

append —— 将 update value 作为子节点追加到目标节点。

delete —— 删除目标节点。

compute —— 将 update value 当作参数化的表达式。表达式中的问号 (?) 将由目标节点的现有文本值替换。然后,计算表达式,得到的值将替换目标节点中的现有值。计算出的值的 xpath 只能是叶节点。

update value —— 这为每个更新命令 (//update/*) 的子节点。它可以是文本节点,也可以是元素。

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

valid value —— 当 @using 属性被设置为 sql 时,子节点应该是文本值。它被当作 sql 表达式。当 @action 属性被设置为 compute 时,子节点应该是文本值。它被当作参数化表达式。在所有其他情况下,子节点都视为要替换的值。

querysql —— 任何用于检索需要更新的 xml 文档的有效 sql 选择语句。

essential —— 是。

valid value —— 只能选择 xml 文档。如果选择了其他列,存储过程就会中止。

updatesql —— 它表示参数化的更新 sql。修改的 xml 文档作为运行时参数被绑定到更新 sql。它允许将修改的 xml 文档保存到数据库中的其他 xml 列中。

essential —— 否。如果该参数为 null,则使用可更新的游标来修改所选的列。

重要事项:从命令行处理器 (clp) 执行更新存储过程时,始终需要设置 updatesql 参数的值。如果将这个值设置为 null 或空字符串,clp 会抛出 jcc 异常:column not updatable。当您从应用程序代码 (java) 内部调用更新存储过程时,将发生此情况(即 updatesql 设置为 null)。

errorcode —— 值为 -1 指示存储过程因发生某种错误而中止。如果更新成功,则返回指示已更新的记录数的正值。

errormsg —— 错误消息,包括 xml 解析器和 jcc 驱动程序抛出的任何异常。

注意:如果得到 java.lang.outofmemoryerror,应该增加 java 堆大小:

db2 update dbm cfg using java_heap_sz 1024

 

设置存储过程

首先需要将更新存储过程 jar 安装到 db2 中。这个过程仅需执行一次。下一步,对于每个数据库,需要分别注册存储过程。

重要事项:如果希望在存储过程中执行 xqueries,那么需要为 db2 设置 jcc 驱动程序。确保 db2 在运行,然后从 db2 命令窗口执行以下命令:

db2set db2_use_db2jcct2_jroutine=on

设置更新存储过程的步骤

通过执行以下步骤编译 java 代码并创建 db2xmlfunctions.jar 文件。

注意:db2xmlfunctions.jar 还可以从 下载 部分下载。如果您选择下载该文件,则跳过 第 2 步。

创建目录 /temp/samples。

将 xmlupdate_code.zip(可以在 下载 部分找到)复制到 temp 目录。

将 xmlupdate.java 和 xmlparse.java 文件解压到 /temp/samples 目录。

编译 java 文件并为 udf 创建 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 设置为 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。根据需要更改为 csh、tsh 等。

在 db2 中安装存储过程:

db2 -t connect to your_dbname&yuml; 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' ; terminate;

删除存储过程

如果更改了存储过程,那么在注册新版本之前应该首先从 db2 卸载它:

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

 

 

xmlupdate 示例

对于 xmlupdate 示例,请执行以下步骤:

创建测试表:

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 ) );

注意:由于更新调用修改了初始的 xml 文档,所以您需要为某些查询而删除插入的文档,并重新插入它。

示例查询

下面是示例查询:

替换节点:action=replace。

通过使用复杂名称元素替换简单名称元素来更新测试文档:

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="//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 文档执行多个操作:

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',?,?);

更新文档时对其进行验证。

为此,您需要创建模式并在 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()。这一步确保即使是空元素(即不具有现有文本节点的元素)也进行更新。如果省略了 text() 且不存在要替换的现有文本值,更新命令就会失败。

使用 xmlupdate 追加子节点。

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',?,?);

注意:新节点 不在任何名称空间中。

使用 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 删除节点。

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',?,?);

当更新元素中没有设置 @action 时,就默认执行替换操作。

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

该查询返回设置为 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',?,?);

该查询返回设置为 1 的错误,以及如下错误消息:

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

使用相同名称空间中的新节点替换某个节点。

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))',?,?);

结束语

本文描述的更新存储过程允许对本机存储在数据库中的 xml 文档进行部分更新。下一篇文章将深度挖掘和具体研究新的 xml 支持对迁移现有基于 xml 的应用程序的重大意义。

致谢

感谢 matthias nicola、bert van der linden、irina kogan、annie wang、ying chen 和 xiaoli du 在撰写这篇文章时给予的帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表