Reputation: 5427
I am trying to use XQuery and BaseX to manage some files.xml as databases.
I am using basexclient to send queries to basexserver. For example I have a simple file called library.xml like the following:
<library>
<book>gone with the wind</book>
<book>the thornbirds</book>
</library>
and I would like to add a node, so from basexclient
terminal I send:
XQUERY insert node <book>Dracula</book> as last into doc('library.xml')//library
and then I get the message
Query executed in 10.5 ms
But nothing has changed into the file. Is there a command to commit changes?
Upvotes: 2
Views: 1182
Reputation: 11
I'm writing a simple sync service between a system and baseX. I have a similar issue in that I'm doing multiple queries using PHP Client and making multiple queries on the same session or multiple that succeed without showing results as viewed from baseX dba. I open, execute, get results if any and close each query. If I execute each query separately, I get the result updated (after a refresh) in dba. I'm running this as a service with port 1894 open to localhost. Each query has the general form of
try {
$myXML22 = 'somegenerated xml'
$filepathname = "mydoc/mydoc_doc22.xml";
$docID = "22";
$dbName = "myDB";
$input = 'let $xml := \''.$myXML22.'\' '.
'return db:replace("'.$dbName.'","'.$filepathname.'",$xml)';
$query = $session->query($input);
$log .= "Updated/added $filepathname to basex db ".$dbName." \n";
} catch (Exception $e) {
error_log("somemessage".$e->getMessage());
}
Here is a logging of the service.
Upvotes: 1
Reputation: 38682
Performant updates to (serialized) XML files are barely possible. BaseX and also the other XML databases on the market all use special, internal representations with indexes for fast querying, and especially updates.
If you already created a database from the file, your query updates the database, not the original XML file. You should be able to observe the changes (possibly multiple times, one node added for each attempt to run the query) in the database representation, which you can verify by running
doc('library.xml')
and checking its contents. To serialize the changes to a file, use BaseX' EXPORT
command.
If you didn't create a database yet and query a file on your hard disk, a temporary in-memory database gets created, which indeed is successfully updated - but instantly discarded. Create a database before running updates.
See Christian Grün's answer discussing the WRITEBACK
option, wich enables you to update a file without creating databases.
Upvotes: 3
Reputation: 6229
By default, updates on a memory instances of your file will not be propagated back to disk. You can change this by turn the WRITEBACK option on. I agree that the behavior is surprising; it was introduced to prevent users from accidentally changing local files.
A general note: If you want to update local files, it may be better to use BaseX in standalone mode. If you are working with the client/server architecture (i.e., with basexclient
and not basex
), the server could have been started from a different working directory, and it may not be able to resolve the relative path in your doc() function.
Upvotes: 4