在企业应用软件设计中,原来往往只需要与一个SQL数据打交道,现在却往往要连接多个数据源,这些数据源可能是SQL数据库、NOSQL数据、XML文件、网站数据等。这么多复杂的数据,有没有办法让它们构成一个数据中心,以相对统一的方式进行管理和数据处理呢?XQuery是合适的前端。
具体做法:
1、在企业服务器上设定数据库连接池,并指定连接名称。
2、选择合适的xquery引擎,并绑定企业服务器。
3、扩展xquery的函数用于操作数据库,比如连接关系数据库,扩展以下函数,原型如下:
declare function sql:select($a as xs:string, $b as xs:string) as node() external;
declare function sql:update($a as xs:string, $b as xs:string) as xs:boolean external;
4、使用xquery查询各单元的数据,合并处理后返回结果。
5、使用xquery维护各数据单元。
这样做有以下的优缺点:
优点:
1、与Sql一样,对外提供统一查询和维护方式。
2、可以以一种统一的方式构建数据中心管理工具。
3、按数据特性,选择合适高效的存储单元。
以后的模式是否应该这样:
一些厂商提供各种具有一定特性的(比如Sql、Cache、Xml、Json、 File)的高性能数据单元。
一些厂商提供高性能的数据中心管理工具,主要的查询和维护语言是xquery,也可延伸sql。
数据库厂商不需要面面俱到,只需要提供的存储单元足够高效就可以。
4、提高SQL数据库的分布性
5、提高内容的分布性。
6、更有效规划内容的存储方式。
缺点:
1、xquery的性能还需要提高。
2、增加了数据存储规划复杂性。
下面是具体使用的例子:
1、查询某类别下产品货号,类别以xml的方式保存在dbxml库,货号放在Sql Server中。
declare function sql:select($a as xs:string, $b as xs:string) as xs:string external;
declare function sql:update($a as xs:string, $b as xs:string) as xs:boolean external;
let $sTxt := xqilla:parse-xml($str)
let $ID := $sTxt/search/@ID
let $t := doc("dbxml:/product/productcategory.dbxml/data")//Category[@ID=$ID]
let $k := data($t/@ID|$t//Category/@ID)
let $s := concat("'",string-join($k,"','"),"'")
let $sql := <Sql>
select rtrim(ItemNo) as Code,Name,Spec from ItemDetail where CategoryID in ({$s})
order by ItemNo
FOR XML PATH ('Product'),ROOT ('Products')
</Sql>
let $link := "link_erp1"
return sql:select($link,$sql/text())
2、使用xquery查询企业资源。
declare namespace fx = "http://local";
declare function sql:select($a as xs:string, $b as xs:string) as node() external;
declare function sql:update($a as xs:string, $b as xs:string) as xs:boolean external;
declare function fx:trim( $arg as xs:string? ) as xs:string
{
replace(replace($arg,'\s+$',''),'^\s+','')
} ;
(:let $ResourceType :="Supplier":)
(:let $ResourceType :="Customer":)
(:let $ResourceType :="Goods":)
let $PageSize := 50
let $PageIndex := 1
let $link := "link_erp1"
return
(
if($ResourceType="Supplier") then
(
let $query := <sql>
DECLARE @PageSize int
DECLARE @PageIndex int
set @PageSize = {$PageSize};
set @PageIndex = {$PageIndex};
WITH Temp AS
(
SELECT ROW_NUMBER() OVER(ORDER BY SupplierNo ASC) AS RowNumber,*
FROM Supplier1
)
SELECT rtrim(SupplierID) as "@guid",
rtrim(SupplierNo) as SupplierNo,
rtrim(SupplierShort) as ShortName,
rtrim(SupplierName) as Name,
rtrim(SupplierAddress) as Address,
rtrim(CountryNo) as Country,
rtrim(ProvinceNo) as Province
FROM Temp
WHERE (RowNumber BETWEEN @PageSize*(@PageIndex-1) + 1 AND @PageSize * @PageIndex )
FOR XML PATH ('Supplier'),ROOT ('Suppliers')
</sql>
return sql:select($link,$query/text())
)
else if($ResourceType="Customer") then
(
let $query := <sql>
DECLARE @PageSize int
DECLARE @PageIndex int
set @PageSize = {$PageSize};
set @PageIndex = {$PageIndex};
WITH Temp AS
(
SELECT ROW_NUMBER() OVER(ORDER BY CustomerNo ASC) AS RowNumber,*
FROM Customer
)
SELECT rtrim(CustomerID) as "@guid",
rtrim(CustomerNo) as CustomerNo,
rtrim(CustomerShort) as ShortName,
rtrim(CustomerName) as Name,
rtrim(CustomerAddress) as Address,
rtrim(Country) as Country
FROM Temp
WHERE (RowNumber BETWEEN @PageSize*(@PageIndex-1) + 1 AND @PageSize * @PageIndex )
FOR XML PATH ('Customer'),ROOT ('Customers')
</sql>
return sql:select($link,$query/text())
)else
()
)