Type of context in remote queries

Monday, 3 November 2014 · 6 messages · last reply 4 Nov 2014
ML
Marco Lettere3 Nov 2014 12:06 #
Hi all, I'm not able to provide myself an answer to the fact that the first query returns an exception (cannot cast string to node) whereas the second works perfectly.
client:connect("localhost", 1984, "admin", "admin") !
   client:query(., "declare context item as node() external; .", {"" :=
<a/>})

client:connect("localhost", 1984, "admin", "admin") !
   client:query(., "declare variable $x as node() external; $x", {"x" :=
<a/>})
I understand that the context is serialized as a string but isn't it the possibility to pass the type information along with the query and avoid explicit serialization on the client and deserialization on the remote server as it happens with variables? Thanks, Marco.
CG
Christian Grün3 Nov 2014 15:23 #
Hi Marco, I have fixed this inconsistency in the latest snapshot [1]. Thanks, Christian [1] http://files.basex.org/releases/latest/
Quoted text · Marco Lettere · 22 lines
On Mon, Nov 3, 2014 at 12:06 PM, Marco Lettere <marco.lettere at dedalus.eu> wrote: > Hi all, > I'm not able to provide myself an answer to the fact that the first query > returns an exception (cannot cast string to node) whereas the second works > perfectly. > > client:connect("localhost", 1984, "admin", "admin") ! > client:query(., "declare context item as node() external; .", {"" := > <a/>}) > > > client:connect("localhost", 1984, "admin", "admin") ! > client:query(., "declare variable $x as node() external; $x", {"x" := > <a/>}) > > I understand that the context is serialized as a string but isn't it the > possibility to pass the type information along with the query and avoid > explicit serialization on the client and deserialization on the remote > server as it happens with variables? > > Thanks, > Marco.
ML
Marco Lettere3 Nov 2014 17:47 #
Nice, Christian. This works! Unfortunately I have to come up with the next question. What if I want to do the same from Java? If from inside an xquery I call a Java extension of mine passing a node (eg: <a/>) as an argument I get the value passed as org.basex.api.dom.BXElem. If I wanted, from inside that Java code, to make a client call towards another basex server I again get the exception because of casting from string to node(). I think my problem is that the bind method of class ClientQuery checks for the value Object being an instance of org.basex.query.value.Value. If it is not and the type is not specified, it serializes the value parameter with toString() which for dom elements like <a/> turns into the string "[a:null]". Unfortunately I'm not able to understand how to transform a org.basex.api.dom.BXNode|BXElem into a org.basex.query.value.Value or possibly specify a type for it in order to make the bind method handle it correctly. Could you please help me out of this? Thank you very much. Marco.
Quoted text · Christian Grün · 33 lines
On 03/11/2014 15:23, Christian Grün wrote: > Hi Marco, > > I have fixed this inconsistency in the latest snapshot [1]. > > Thanks, > Christian > > [1] http://files.basex.org/releases/latest/ > > > On Mon, Nov 3, 2014 at 12:06 PM, Marco Lettere <marco.lettere at dedalus.eu> wrote: >> Hi all, >> I'm not able to provide myself an answer to the fact that the first query >> returns an exception (cannot cast string to node) whereas the second works >> perfectly. >> >> client:connect("localhost", 1984, "admin", "admin") ! >> client:query(., "declare context item as node() external; .", {"" := >> <a/>}) >> >> >> client:connect("localhost", 1984, "admin", "admin") ! >> client:query(., "declare variable $x as node() external; $x", {"x" := >> <a/>}) >> >> I understand that the context is serialized as a string but isn't it the >> possibility to pass the type information along with the query and avoid >> explicit serialization on the client and deserialization on the remote >> server as it happens with variables? >> >> Thanks, >> Marco.
CG
Christian Grün4 Nov 2014 00:09 #
Quoted text · Marco Lettere · 1 line
> What if I want to do the same from Java?
Spontaneously, I get two alternatives in mind (I guess there are some more): DOM dom = ...your dom element... a) Converting to internal BaseX node type (adopted from [1]):
  FElem elem = new FElem((Element) dom, null, new TokenMap());
  query.bind("var", elem);
  ...
b) Serializing as string, specifying the original type in the bind function:
  Transformer t = TransformerFactory.newInstance().newTransformer();
  StringWriter sw = new StringWriter();
  t.transform(new DOMSource(dom), new StreamResult(sw));
  query.bind("var", sw, "element()");
  ...
CG
Christian Grün4 Nov 2014 00:44 #
Here are two more solutions: c) Retrieve internal node representation from BXNode instance: query.bind("var", bxnode.getNode()); d) Use the latest snapshot ;)
ML
Marco Lettere4 Nov 2014 10:11 #
Quoted text · Christian Grün · 8 lines
On 04/11/2014 00:44, Christian Grün wrote: > Here are two more solutions: > > c) Retrieve internal node representation from BXNode instance: > > query.bind("var", bxnode.getNode()); > > d) Use the latest snapshot ;)
YEAH! This last answer is the one I like most ;-) And, "ça va sans dire", it works! Thanks, M.

⚡Generated with XQuery