Hi Graydon,

The W3 XQuery Update Facility was designed to operate without side effects.
Update operations are not immediately executed, but added to the so-called
»Pending Update List« (also called PUL). After the evaluation of the query,
the update operations are checked for conflicts and inconsistencies before
they are eventually executed [1].

As a consequence, it is not possible to query a database after a db:create
function call, as this would introduce side effects to the language: A
db:open function call on that database would yield different results before
and after the database creation.

Instead, as suggested by Johan, you can write command scripts. If you don’t
need a permanent database, you can also bind documents or collections to
variables and perform queries on that data:

  let $docs := collection('/path/to/files')
  return (
    file:write('names.xml', element names { $docs//names }),
    proc:execute(...)
  )

My example indicates that there are lots of functions in BaseX that are
indeed side-effecting and non-deterministic, and do not comply with the
functional semantics of XQuery Update. It would have been possible to use
the PUL for all operations in BaseX that are (possibly) updating data, but
we decided this would have been too restrictive. However, we did define all
custom updating database functions as PUL operations, as this allows for
much better transactional and locking semantics, which is particularly
important if multiple users are accessing data that is being read and
written at the same time. And it prevents us from coping with all kinds of
contradictory queries that could lead to errors. See e.g:

  let $db := db:open('x')
  return (db:create('x'), $db//hm-i-just-was-overwritten-by-a-new-database)

Hope this helps,
Christian

PS: The W3 XQuery Scripting Extension was designed to support subsequent
and independent reading and updating operations [2]. As it was pretty
complex and didn’t provide solutions for all functional
restrictions/challenges you would expect from a scripting language, the
developers from the Zorba XQuery Processor were the only ones that decided
to implement it.

[1] https://docs.basex.org/wiki/XQuery_Update#Concepts
[2] https://www.w3.org/TR/xquery-sx-10/

Reply via email to