Root?

Tuesday, 4 January 2011 · 5 messages
CM
Charles F. Munat4 Jan 2011 01:55 #
I have an embedded Base-X database in a web application. I have a huge XQuery that involves a lot of local functions, many of them recursive. In several of these functions, I need to query the database to look up referenced elements. I can't for the life of me figure out a way to access the database without doing one of two things: 1. My document root is /data. I can let $root := /data and then pass that into every function. Passing things into functions rather than setting global variable is certainly and FP way of doing things, but in this instance it seems to me that it should be the scope I'm working in. 2. If something else I pass into the function is a node in the database, then I can, for example, use $node/root() to back up to the root node. But sometimes I'm only passing in a string and I still need to query the whole database. Is there some way to access the root easily from within a function? I am very confused by the whole "context" idea. I've looked all over, but I must be searching using the wrong terms, or it simply isn't done. Anyone know a way? Thanks, Chas.
AW
Andreas Weiler4 Jan 2011 09:47 #
Hi Chas, i actually don't understand what your concrete problem is, but you could have a look in our xquery database functions: http://www.inf.uni-konstanz.de/dbis/basex/xq-db If you have opened the database before running queries, the whole database is in your context and so it is used in the queries. Kind regards, Andreas
Quoted text · Charles F. Munat · 31 lines
Am 04.01.11 00:54, schrieb Charles F. Munat: > I have an embedded Base-X database in a web application. I have a huge > XQuery that involves a lot of local functions, many of them recursive. > In several of these functions, I need to query the database to look up > referenced elements. I can't for the life of me figure out a way to > access the database without doing one of two things: > > 1. My document root is /data. I can let $root := /data and then pass > that into every function. Passing things into functions rather than > setting global variable is certainly and FP way of doing things, but > in this instance it seems to me that it should be the scope I'm > working in. > > 2. If something else I pass into the function is a node in the > database, then I can, for example, use $node/root() to back up to the > root node. But sometimes I'm only passing in a string and I still need > to query the whole database. > > Is there some way to access the root easily from within a function? I > am very confused by the whole "context" idea. I've looked all over, > but I must be searching using the wrong terms, or it simply isn't done. > > Anyone know a way? > > Thanks, > > Chas. > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
MS
Michael Seiferle4 Jan 2011 12:48 #
Hey Chas, Hey Andi, Andis Mail pointed me into the right direction and I guess you could simply do the following, instead of passing around the db nodes
Quoted text · Andreas Weiler · 10 lines
> declare variable $_context := doc('data'); > declare function local:getPersonFromID($id) { > $_context//persons/person[@id=$id] > }; > local:getPersonFromID('1') Or you could as well pass the db name and open it dynamically in your function > declare function local:getPersonFromID($id, $dbn) { > doc($dbn)//persons/person[@id=$id] > }; > local:getPersonFromID('1','data')
The current context is never implicitly passed on to functions. This is to avoid side effects; so a function is only defined by its signature. Without doubt the solution I propose here should not be taken as a best practice but probably suits your case well enough. There probably are some better solutions around. Maybe s/o else on the list knows of one. Hope this helps! In case it does not, feel free to ask for more. Kind regards Michael
Quoted text · Andreas Weiler · 35 lines
Am 04.01.2011 um 09:47 schrieb Andreas Weiler: > Hi Chas, > > i actually don't understand what your concrete problem is, but you could have a look > in our xquery database functions: http://www.inf.uni-konstanz.de/dbis/basex/xq-db > > If you have opened the database before running queries, the whole database is in your context and so it is used > in the queries. > > Kind regards, > Andreas > > Am 04.01.11 00:54, schrieb Charles F. Munat: >> I have an embedded Base-X database in a web application. I have a huge XQuery that involves a lot of local functions, many of them recursive. In several of these functions, I need to query the database to look up referenced elements. I can't for the life of me figure out a way to access the database without doing one of two things: >> >> 1. My document root is /data. I can let $root := /data and then pass that into every function. Passing things into functions rather than setting global variable is certainly and FP way of doing things, but in this instance it seems to me that it should be the scope I'm working in. >> >> 2. If something else I pass into the function is a node in the database, then I can, for example, use $node/root() to back up to the root node. But sometimes I'm only passing in a string and I still need to query the whole database. >> >> Is there some way to access the root easily from within a function? I am very confused by the whole "context" idea. I've looked all over, but I must be searching using the wrong terms, or it simply isn't done. >> >> Anyone know a way? >> >> Thanks, >> >> Chas. >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk > > _______________________________________________ > BaseX-Talk mailing list > BaseX-Talk at mailman.uni-konstanz.de > https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
AW
Andreas Weiler4 Jan 2011 12:57 #
-------- Original-Nachricht -------- Betreff: Re: [basex-talk] Root? Datum: Tue, 04 Jan 2011 08:56:13 -0300
Quoted text · Charles F. Munat · 164 lines
Von: Charles F. Munat <charles.munat at gmail.com> An: Andreas Weiler <andreas.weiler at uni-konstanz.de> Actually, this is perfect. Exactly what I was trying to do -- set a global variable. I must have had the syntax wrong, because it wasn't working. But this is great. One less thing to pass around. When you're query involves a couple dozen functions, it gets tiresome. Thanks much for all your help! This makes it a lot easier. Chas. On 01/04/2011 8:24 AM, Andreas Weiler wrote: > Hi Chas, > > thanks for you example, that made it easy to understand. > The database context isn't available in own declared functions and > variables. > So you could use the doc('database') function and run the following query: > > declare variable $data as node() := doc('database')/data; > > declare function local:getPersonFromID($id) { > $data/persons/person[@id=$id] > }; > > local:getPersonFromID('1') > > So it's almost the same than your workaround. > The doc('database') function gives you access to your database. > > I'm sorry there is no easier way, > kind regards, > Andreas > > Am 04.01.11 11:26, schrieb Charles F. Munat: >> Sorry, it's hard to explain these things since I have no idea what, if >> anything, I'm doing wrong. Here's a basic example. >> >> I create a database in code: >> >> (I'm using Scala, but hopefully this is close enough so you can see >> what I'm doing.) >> >> private val context = new Context() >> new org.basex.core.cmd.Set("dbpath", "/var/www/xxx/db").execute(context) >> >> try { >> new Open("data").execute(context) >> } catch { >> case _ => new CreateDB("data", "<data/>").execute(context) >> } >> >> So now I have a new database that contains only<data/>. I now load it: >> >> val file = "/var/www/xxx/content/persons.xml" >> new XQuery("insert node " + >> XML.loadFile(file).toString + >> " into /data").execute(context) >> >> Now my data is in the embedded database. Suppose the file looked like >> this: >> >> <persons> >> <person id="1"> >> <names> >> <given>John</given> >> <family>Lennon</family> >> </names> >> </person> >> <person> >> <names id="2"> >> <given>Paul</given> >> <family>McCartney</family> >> </names> >> </person> >> </persons> >> >> Now I can run an XQuery like this (really just XPath): >> >> /data/persons/person[@id='1'] >> >> And I'll get back: >> >> <person id="1"> >> <names> >> <given>John</given> >> <family>Lennon</family> >> </names> >> </person> >> >> Simple, right? >> >> But if I do this: >> >> declare function local:getPersonFromID($id) { >> /data/persons/person[@id=$id] >> }; >> >> local:getPersonFromID('1') >> >> Then I get this error: >> >> No context item set for 'root()'. >> >> The workaround I've been using is this: >> >> declare function local:getPersonFromID($data, $id) { >> $data/persons/person[@id=$id] >> }; >> >> let $data := /data >> return local:getPersonFromID('1') >> >> There must be a better way. >> >> Does this make more sense? >> >> Chas. >> >> On 01/04/2011 5:47 AM, Andreas Weiler wrote: >>> Hi Chas, >>> >>> i actually don't understand what your concrete problem is, but you could >>> have a look >>> in our xquery database functions: >>> http://www.inf.uni-konstanz.de/dbis/basex/xq-db >>> >>> If you have opened the database before running queries, the whole >>> database is in your context and so it is used >>> in the queries. >>> >>> Kind regards, >>> Andreas >>> >>> Am 04.01.11 00:54, schrieb Charles F. Munat: >>>> I have an embedded Base-X database in a web application. I have a huge >>>> XQuery that involves a lot of local functions, many of them recursive. >>>> In several of these functions, I need to query the database to look up >>>> referenced elements. I can't for the life of me figure out a way to >>>> access the database without doing one of two things: >>>> >>>> 1. My document root is /data. I can let $root := /data and then pass >>>> that into every function. Passing things into functions rather than >>>> setting global variable is certainly and FP way of doing things, but >>>> in this instance it seems to me that it should be the scope I'm >>>> working in. >>>> >>>> 2. If something else I pass into the function is a node in the >>>> database, then I can, for example, use $node/root() to back up to the >>>> root node. But sometimes I'm only passing in a string and I still need >>>> to query the whole database. >>>> >>>> Is there some way to access the root easily from within a function? I >>>> am very confused by the whole "context" idea. I've looked all over, >>>> but I must be searching using the wrong terms, or it simply isn't done. >>>> >>>> Anyone know a way? >>>> >>>> Thanks, >>>> >>>> Chas. >>>> _______________________________________________ >>>> BaseX-Talk mailing list >>>> BaseX-Talk at mailman.uni-konstanz.de >>>> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >>> >
CM
Charles F. Munatin reply to Michael Seiferle, 4 Jan 2011, 12:484 Jan 2011 14:08 #
Thanks! I prefer the first option. It gives me access without having to pass something on every function call. Good point about the side effects; I'll have to keep that in mind. Chas.
Quoted text · Michael Seiferle · 67 lines
On 01/04/2011 8:48 AM, Michael Seiferle wrote: > Hey Chas, Hey Andi, > > Andis Mail pointed me into the right direction and I guess you could simply do the following, instead of passing around the db nodes > >> declare variable $_context := doc('data'); >> declare function local:getPersonFromID($id) { >> $_context//persons/person[@id=$id] >> }; >> local:getPersonFromID('1') > > Or you could as well pass the db name and open it dynamically in your function >> declare function local:getPersonFromID($id, $dbn) { >> doc($dbn)//persons/person[@id=$id] >> }; >> local:getPersonFromID('1','data') > > > The current context is never implicitly passed on to functions. > This is to avoid side effects; so a function is only defined by its signature. > Without doubt the solution I propose here should not be taken as a best practice but probably suits your case well enough. > > There probably are some better solutions around. Maybe s/o else on the list knows of one. > > Hope this helps! In case it does not, feel free to ask for more. > > Kind regards > > Michael > > Am 04.01.2011 um 09:47 schrieb Andreas Weiler: > >> Hi Chas, >> >> i actually don't understand what your concrete problem is, but you could have a look >> in our xquery database functions: http://www.inf.uni-konstanz.de/dbis/basex/xq-db >> >> If you have opened the database before running queries, the whole database is in your context and so it is used >> in the queries. >> >> Kind regards, >> Andreas >> >> Am 04.01.11 00:54, schrieb Charles F. Munat: >>> I have an embedded Base-X database in a web application. I have a huge XQuery that involves a lot of local functions, many of them recursive. In several of these functions, I need to query the database to look up referenced elements. I can't for the life of me figure out a way to access the database without doing one of two things: >>> >>> 1. My document root is /data. I can let $root := /data and then pass that into every function. Passing things into functions rather than setting global variable is certainly and FP way of doing things, but in this instance it seems to me that it should be the scope I'm working in. >>> >>> 2. If something else I pass into the function is a node in the database, then I can, for example, use $node/root() to back up to the root node. But sometimes I'm only passing in a string and I still need to query the whole database. >>> >>> Is there some way to access the root easily from within a function? I am very confused by the whole "context" idea. I've looked all over, but I must be searching using the wrong terms, or it simply isn't done. >>> >>> Anyone know a way? >>> >>> Thanks, >>> >>> Chas. >>> _______________________________________________ >>> BaseX-Talk mailing list >>> BaseX-Talk at mailman.uni-konstanz.de >>> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >> >> _______________________________________________ >> BaseX-Talk mailing list >> BaseX-Talk at mailman.uni-konstanz.de >> https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk >

⚡Generated with XQuery