I'm trying to use BaseX as a persistence layer in a project (together with XStream). I have a few questions and I'm not having any luck with the documentation.
I've gotten pretty far. I have a User object (simplified for this example) with an id (UUID), a username (String), and a password (String). XStream turns this into:
<User> <id>98bdb472-97ad-4de1-b1c6-a482d7f06d94</id> <username>Chas</username> <password>secret</password> </User>
I've created a DB called "db" (clever, huh?):
new CreateDB("cop", "<blog><users/></blog>").execute(context)
QUESTION: If I rerun the above, will it overwrite the DB if it already exists, or will it just open the existing DB? Can't find anything in the API about that.
Then I save my User like this:
new XQuery("insert node" + chas + " into /blog/users").execute(context)
where chas is the above XML.
This works great! When I need to reinflate the object, I just do:
chas = new XQuery(""" for $x in /forum/users/User where $x/username="Chas" return $x """).execute(context)
Then I use XStream to reinflate the object.
Any suggestions for improvement?
So now the problem. I need to update this object in the database. The simplest way is just to update the whole object. So I change my username and use XStream to create the new XML:
<User> <id>98bdb472-97ad-4de1-b1c6-a482d7f06d94</id> <username>Charles</username> <password>secret</password> </User>
I read that one must use put() to export the changes, but maybe I'm misunderstanding this. I'm have this:
for $x in /forum/users/User where $x/username="Chas" return replace node $x with " + chas
This and seems to work, but then when I try to delete the node thus:
for $x in /forum/users/User where $x/password="secret" return delete nodes $x
It does not work. What am I missing?
Thanks for any help. If this thing works, I'm happy to open it up so others can work on it, too.
Chas.