Best way to understand Query result type
Thursday, 11 September 2014 · 2 messages · last reply 12 Sep 2014
ML
Marco Lettere11 Sep 2014 11:07 #
Hello all,
I'm in the situation where I'd have to execute an XQuery on a different Server from an external Java code (check the small example provided). What is the best way of transforming the resulting bytestream into BaseX' Value model (item()*, node()*, xs:string* whatever is returned by the query) in order to be able to treat the result as if it was generated inside the calling XQuery itself?
Would the HTTPClient that ships with Basex be of any help? Or do I have to do everything manually?
I hope the question is stated clearly enough anyway here is a short example (probably not working directly ;-)) that demonstrates the concept.
declare namesapce ext = "my.externalClass.EC";
let $out := ext:eval("http://otherhost:8984/rest" "{1 to 10 ! <a/>}")
return <total count={count($out)}>
{$out}
</total>EC has few methods most importantly:
private String queryToXML(String query){
return
"<query xmlns=\"http://basex.org/rest\">" +
"<text><![CDATA[" + query + "]]></text>" +
"</query>";
}
public static void eval(String address, String query) {
try {
URL url = new URL(address);
HttpURLConnection conn =
(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.getOutputStream().write(this.query.getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf,0,buf.length);
while(len != -1){
baos.write(buf, 0, len);
len = is.read(buf,0,buf.length);
}
is.close();
byte[] = baos.toByteArray();
baos.close();
} catch (Exception e) {
result = e;
}
}The Question translates to what should eval's return type be and how to generate it from ByteArrayOutputStream.
Thank you.
M.
CG
Christian Grün12 Sep 2014 17:28 #
Hi Marco,
you should probably check out the ClientSession and ClientQuery classes [1, 2], which are also utilized by the XQuery Client Module [3]. The returned Type IDs [4] indicate which type a single item has.
Does this help?
Christian
[1] https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/api/client/ClientSession.java [2] https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/api/client/ClientQuery.java [3] http://docs.basex.org/wiki/Client_Module
[4] http://docs.basex.org/wiki/Server_Protocol:_Types
Quoted text · Marco Lettere · 63 lines
On Thu, Sep 11, 2014 at 11:07 AM, Marco Lettere <marco.lettere at dedalus.eu> wrote: > Hello all, > I'm in the situation where I'd have to execute an XQuery on a different > Server from an external Java code (check the small example provided). > What is the best way of transforming the resulting bytestream into BaseX' > Value model (item()*, node()*, xs:string* whatever is returned by the query) > in order to be able to treat the result as if it was generated inside the > calling XQuery itself? > Would the HTTPClient that ships with Basex be of any help? Or do I have to > do everything manually? > > I hope the question is stated clearly enough anyway here is a short example > (probably not working directly ;-)) that demonstrates the concept. > > declare namesapce ext = "my.externalClass.EC"; > > let $out := ext:eval("http://otherhost:8984/rest" "{1 to 10 ! <a/>}") > return <total count={count($out)}> > {$out} > </total> > > EC has few methods most importantly: > > private String queryToXML(String query){ > return > "<query xmlns=\"http://basex.org/rest\">" + > "<text><![CDATA[" + query + "]]></text>" + > "</query>"; > } > > public static void eval(String address, String query) { > try { > > URL url = new URL(address); > HttpURLConnection conn = > (HttpURLConnection)url.openConnection(); > conn.setRequestMethod("POST"); > conn.setDoOutput(true); > conn.getOutputStream().write(this.query.getBytes()); > > ByteArrayOutputStream baos = new ByteArrayOutputStream(); > InputStream is = conn.getInputStream(); > byte[] buf = new byte[1024]; > int len = is.read(buf,0,buf.length); > while(len != -1){ > baos.write(buf, 0, len); > len = is.read(buf,0,buf.length); > } > is.close(); > byte[] = baos.toByteArray(); > baos.close(); > } catch (Exception e) { > result = e; > } > } > > The Question translates to what should eval's return type be and how to > generate it from ByteArrayOutputStream. > > Thank you. > M. >
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery