使用XQuery Update自动递增? [英] Auto increment with XQuery Update?

查看:89
本文介绍了使用XQuery Update自动递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XQuery更新是否支持自动递增属性,就像SQL中的自动递增字段一样?

Does XQuery Update support auto increment attributes, just like auto increment fields in SQL?

我正在使用BaseX作为数据库.

I'm using BaseX as my database.

推荐答案

给出来自BaseX邮件列表上的ChristianGrün的答案,这在XQuery Update语句中定义了要添加的节点时是可行的,因此可以使用{enclosed expression} 在插入之前:

Given an answer from Christian Grün on the BaseX mailing list, this is doable when the node one is adding is defined in the XQuery Update statement, and hence can be enhanced using an {enclosed expression} before inserting it:

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

You might specify the attribute counter within your XML file/database and increment it every time when you insert an element. A simple example:

input.xml:

input.xml:

<root count="0"/>

insert.xq:

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数据库中,我无法实现相同的目的.在这里,添加文档后可能会尝试更新自动增量"数据.但是,处理模型定义了直到所有命令都已排队才可见更改(待更新列表).因此,根据定义,对于同一FLWOR表达式中的更新,新文档或节点根本不可见.所以:

I've not been able to achieve the same with an external org.w3c.dom.Document created in Java, and added to the XML database using XQJ and declare variable $doc external. Here, one might be tempted to update the "auto-increment" data after adding the document. However, the processing model defines that changes are not visible until all the commands have been queued (the Pending Update List). Hence a new document or node is, by definition, simply not visible for updates in the same FLWOR expression. So:

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

...以下重复执行以下命令将不起作用:

...followed by repetitive executions of the following, will NOT work:

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="">,并且直到添加下一个文档后才会更新. (此外,当存在多个带​​有<note id="">的文档时,它将不起作用.)

Above, the last inserted document will always have <note id="">, and will not be updated until the next document is added. (Also, it would not work when somehow multiple documents with <note id=""> would exist.)

尽管在上面的示例中,可以成功删除for $note in ...部分并使用:

Though in the example above one could successfully delete the for $note in ... part and use:

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

...我在Java代码的文档

...I had no luck setting <note id="{ $count }"> in the Document in the Java code, as that enclosed expression would not be replaced then.

最后,一些表示类似解决方案的状态:

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

[...] perform badly as it will lock out concurrent updates. You should consider using xdmp:random() to generate a 64 bit random number for your unique identifier.

在我们的示例中,该ID也将用在网址中;那就不太好了.

In our case, the id would also be used in URLs; not too nice then.

另请参见 XRX/自动增量文件ID 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆