krunal
krunal

Reputation: 482

How do queries actually run in eXist-db (sandbox vs. stored query)

I am working with eXist-db. What are the processes on the back end of eXist-db when you execute a query? For example, how do the queries run differently in the following scenarios?

  1. While executing a query through the eXist Sandbox?

  2. While executing a query through the REST interface (i.e., through a web browser)?

  3. Also, what happens on the back end when transforming a CSV File to XML file located in the same collection? (i.e. using the steps outlined here. In my case, I stored my CSV file in the database. What happens to the following files:

    • CSV file
    • XQuery file
    • XML file resulting from running the CSV file through the XQuery transformation

Upvotes: 1

Views: 969

Answers (1)

Joe Wicentowski
Joe Wicentowski

Reputation: 5294

  1. The eXist XQuery Sandbox is a webpage with a text field and a submit button. When you enter your query and submit the query, the contents of the text field are sent to the sandbox.xql query on the server via Ajax, and the server dynamically executes the query (using util:eval()). The sequence of results is then stored in the session. The first ten items in the result sequence are then displayed in the results window; you can page through the results 10 at a time. By storing the results in the session, paging through the results doesn't require re-submitting the query. The next time you submit a query, your old results are cleared, and the new results are inserted into the session.

  2. When you execute a query through the REST interface (i.e., by pointing your web browser directly at a query is stored in the database, such as a .xq, .xqy, .xql, or .xquery file). The database reads the file and executes the query, directly returning all results directly to the browser.

Note that while the Sandbox doesn't complain if your query returns multiple results, a browser does expect XML documents to be well-formed -- i.e. to have a single root element. A common mistake or misunderstanding that occurs when you move a query from the Sandbox to a stored .xq file is to forget to wrap the query results in a single root element.

For example, in the Sandbox, you can enter this as a query:

<x/>, <y/>, <z/>

When you submit it, you'll get 3 results:

  1. <x/>
  2. <y/>
  3. <z/>

But if you save this as a .xq file (e.g., myquery.xq) and call this via http://localhost:8080/exist/rest/db/myquery.xq, then you will get an error in the browser about the results not being well-formed. The solution is to wrap the results in a root element, e.g.:

<results><x/><y/><z/></results>

Your last question is a little vague, but did you see my answer to your earlier question about how to run CSV conversion? I outlined how to write a query to get the CSV file stored in the database, transform the CSV file into XML, and save the results as an XML file in the database.

Upvotes: 0

Related Questions