简体   繁体   English

如何在BaseX中返回结果以及更新操作?

[英]How to return results together with update operations in BaseX?

I recognized that ( insert / delete )-XQueries executed with the BaseX client always returning an empty string. 我认识到,用BaseX客户端执行的( insert / delete )-XQueries总是返回空字符串。 I find this very confusing or unintuitive. 我觉得这很混乱或不直观。

Is there a way to find out if the query was "successful" without querying the database again (and using potentially buggy "transitive" logic like "if I deleted a node, there must be 'oldNodeCount-1' nodes in the XML")? 有没有一种方法可以找到查询是否“成功”而无需再次查询数据库(并使用可能有问题的“传递”逻辑,例如“如果我删除了一个节点,那么XML中必须有“ oldNodeCount-1”个节点”) ?

XQuery Update statements do not return anything -- that's how they are defined. XQuery Update语句不返回任何内容-这就是它们的定义方式。 But you're not the only one who does not like those restrictions, and BaseX added two ways around this limitation : 但是您不是唯一不喜欢这些限制的人,而BaseX围绕此限制添加了两种方法

Returning Results 返回结果

By default, it is not possible to mix different types of expressions in a query result. 默认情况下,不可能在查询结果中混合使用不同类型的表达式。 The outermost expression of a query must either be a collection of updating or non-updating expressions. 查询的最外层表达式必须是更新表达式或非更新表达式的集合。 But there are two ways out: 但是有两种方法可以解决:

  • The BaseX-specific update:output() function bridges this gap: it caches the results of its arguments at runtime and returns them after all updates have been processed. 特定于BaseX的update:output()函数弥合了这一差距:它在运行时缓存其参数的结果,并在处理所有更新后返回它们。 The following example performs an update and returns a success message: 以下示例执行更新并返回成功消息:

     update:output("Update successful."), insert node <c/> into doc('factbook')/mondial 
  • With the MIXUPDATES option, all updating constraints will be turned off. 使用MIXUPDATES选项,将关闭所有更新约束。 Returned nodes will be copied before they are modified by updating expressions. 返回的节点将被复制,然后通过更新表达式进行修改。 An error is raised if items are returned within a transform expression. 如果在转换表达式中返回项目,则会引发错误。

If you want to modify nodes in main memory, you can use the transform expression . 如果要修改主内存中的节点,可以使用transform表达式

The transform expression will not help you, as you seem to modify the data on disk . 转换表达式将无济于事,因为您似乎在修改磁盘上的数据。 Enabling MIXUPDATES allows you to both update the document and return something at the same time, for example running something like 启用MIXUPDATES使您既可以更新文档又可以同时返回某些内容,例如运行类似

let $node := <c/>
return ($node, insert node $node into doc('factbook')/mondial)

MIXUPDATES allows you to return something which can be further processed. MIXUPDATES允许您返回可以进一步处理的内容。 Results are copied before being returned, if you run multiple updates operations and do not get the expected results, make sure you got the concept of the pending update list . 结果将在返回之前被复制,如果您运行多个更新操作并且未获得预期的结果,请确保您已获得挂起的更新列表的概念。

The db:output() function intentionally breaks its interface contract: it is defined to be an updating function (not having any output), but at the same time it prints some information to the query info. db:output()函数有意破坏其接口协定:它被定义为一个更新函数(不具有任何输出),但同时它将一些信息打印到查询信息中。 You cannot further process these results, but the output can help you debugging some issues. 您无法进一步处理这些结果,但是输出可以帮助您调试一些问题。

Pending Update List 待更新列表

Both ways, you will not be able to have an immediate result from the update, you have to add something on your own -- and be aware updates are not visible until the pending update list is applied, ie. 两种方式都将无法立即获得更新的结果,您必须自己添加一些内容-并且要注意,直到应用了挂起的更新列表(例如)时,更新才可见。 after the query finished. 查询完成后。

Compatibility 兼容性

Obviously, these options are BaseX-specific. 显然,这些选项是特定于BaseX的。 If you strongly require compatible and standard XQuery, you cannot use these expressions. 如果您强烈要求兼容和标准的XQuery,则不能使用这些表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM