Does XQuery
Update support auto increment attributes, just like auto increment fields in SQL
?
I'm using BaseX
as my database.
Does XQuery
Update support auto increment attributes, just like auto increment fields in SQL
?
I'm using BaseX
as my database.
This would depend on the implementation of the underlying data-store, because the auto-increment attribute is on the column definition in relational databases.
Probably, "yes".
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: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 anddeclare 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:...followed by repetitive executions of the following, will NOT work:
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.)Though in the example above one could successfully delete the
for $note in ...
part and use:...I had no luck setting
<note id="{ $count }">
in the Document in the Java code, as that enclosed expression would not be replaced then.Finally, some state for similar solutions:
In our case, the id would also be used in URLs; not too nice then.
See also XRX/Autoincrement File ID and Using XQuery to return Document IDs.