2010-04-01 53 views
2

是否XQuery更新支持自动增量属性,就像在SQL自动递增的字段?使用XQuery更新自动递增?

我用BaseX作为我的数据库。

+0

你有没有解决过这个问题? – Arjan 2012-09-30 12:49:37

+0

不,我必须在应用程序中而不是在数据库中执行此操作。 – 2012-09-30 13:17:09

+0

...然后使用其他数据库为您生成唯一的ID?由于XQuery更新语句不能与返回值相结合,我甚至无法在XML数据库中实现一些“数据库序列”...... – Arjan 2012-09-30 14:31:25

回答

1

这将取决于基础数据存储的实现,这是因为自动增量属性是在关系数据库中的列定义。

也许, “是的”。

+0

感谢您的回答。我在这个问题中增加了更多信息。 – 2010-04-08 08:36:34

2

鉴于an answer from Christian Grün on the BaseX mailing list,这是可行的,当节点之一将是在XQuery更新语句定义,因此可以使用增强的{enclosed expression}之前将其插入:

你可以指定属性计数器在您的XML文件/数据库 中,并在每次插入元素时递增。一个简单的例子 :

的input.xml:

<root count="0"/> 

insert.xq:

let $root := doc('input.xml')/root 
let $count := $root/@count 
return (
    insert node <node id='{ $count }'/> into $root, 
    replace value of node $count with $count + 1 
) 

我已经无法达到同样的在Java中创建一个外部org.w3c.dom.Document并使用XQJ和declare variable $doc external将其添加到XML数据库中。在这里,可能会试图在添加文档后更新“自动增量”数据。但是,处理模型定义在所有命令排队之前,更改不可见(Pending Update List)。因此,根据定义,新文档或节点根本不可见于同一FLWOR表达式中的更新。所以:

db:add('db1', '<counters comment-id="0"/>', 'counters') 

...后跟以下的重复执行,则不行:

let $doc := document{ <note id=""><text>Hello world</text></note> } 
let $count := /counters/@comment-id 
return (
    db:add('db1', $doc, 'dummy'), 
    replace value of node $count with $count + 1 
    for $note in /note[@id=''] 
    return replace value of node $note/@id with $count (: WRONG! :) 
) 

以上,最后插入的文件将永远有<note id="">,并不会直到下一次更新文件被添加。在上面的例子中一个可以成功地删除for $note in ...部分和使用(而且,它不会当会存在某种方式与<note id="">多个文档。)

虽然:

let $doc := document{ <note id="{ $count }"><text>Hello world</text></note> } 

...我没有运气在Java代码as that enclosed expression would not be replaced then的文档中设置<note id="{ $count }">

最后,一些state for similar solutions

[...]表现不佳,因为它会锁定并发更新。您应该考虑使用xdmp:random()为您的唯一标识符生成一个64位随机数。

在我们的情况下,ID也将在URL中使用;然后不太好。请参阅XRX/Autoincrement File IDUsing XQuery to return Document IDs

+1

文档的主内存更新可能对此有所帮助;请参阅http://stackoverflow.com/questions/12697957/can-i-make-xquery-evaluate-expression-within-variables-bound-with-xqj/13212103#comment17990896_13212103了解更多信息。 – 2012-11-03 18:27:46