XCC/J 4.1-11

com.marklogic.xcc.examples
Class SimpleQueryRunner

java.lang.Object
  extended by com.marklogic.xcc.examples.SimpleQueryRunner

public class SimpleQueryRunner
extends Object

This is a very simple class that will submit an XQuery string to the server and return the result.

Click here for the source code for this class

The main() method looks for two command-line args, a URL for the server ( ContentSourceFactory.newContentSource(java.net.URI)) and a filename to read a query from. It loads the file, submits its contents to the server for evaluation and then prints the result sequence to stdout, one item per line.

The class has methods the could be used to set request options and to obtain the results as an array of Strings or as a real ResultSequence.

If you want to set external variables for a request, you can call getRequest() to obtain a reference to the internal Request object and set the variable values on it before invoking execute(String).


Constructor Summary
SimpleQueryRunner(URI serverUri)
          Construct an instance that will submit query requests to the server represented by the given URI.
 
Method Summary
 ResultSequence execute(String query)
          Submit the given query string and return a ResultSequence object.
 String executeToSingleString(String query, String separator)
          Submit the given query string and return a single String which is the concatenation of all the ResultItems, separated by the given separator string.
 String[] executeToStringArray(String query)
          Submit the given query string and return an array of Strings, possibly of length zero, which contains the String value of each ResultItem (see XdmValue.asString())
 Request getRequest()
          Returns the Request object used internally to submit requests.
static void main(String[] args)
          Simple command-line driver that will read a query from a file and submit it to the server at the given URL.
 void setRequestOptions(RequestOptions options)
          Set (or clear) the RequestOptions instance to associate with submitted queries.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleQueryRunner

public SimpleQueryRunner(URI serverUri)
                  throws XccConfigException
Construct an instance that will submit query requests to the server represented by the given URI. Note that the URI will not be validated at this time.

Parameters:
serverUri - A URI that specifies a server per ( ContentSourceFactory.newContentSource(java.net.URI)).
Throws:
XccConfigException - If the URI is not a valid XCC server URL.

Method Detail

execute

public ResultSequence execute(String query)
                       throws RequestException
Submit the given query string and return a ResultSequence object.

Parameters:
query - XQuery code as a String, to be evaluated by the server.
Returns:
An instance ResultSequence, possibly with size zero.
Throws:
RequestException - If an unrecoverable error occurs when submitting or evaluating the request.


executeToStringArray

public String[] executeToStringArray(String query)
                              throws RequestException
Submit the given query string and return an array of Strings, possibly of length zero, which contains the String value of each ResultItem (see XdmValue.asString())

Parameters:
query - XQuery code as a String, to be evaluated by the server.
Returns:
An array of Strings, one per item in the ResultSequence.
Throws:
RequestException - If an unrecoverable error occurs when submitting or evaluating the request.


executeToSingleString

public String executeToSingleString(String query,
                                    String separator)
                             throws RequestException
Submit the given query string and return a single String which is the concatenation of all the ResultItems, separated by the given separator string.

Parameters:
query - XQuery code as a String, to be evaluated by the server.
separator - A String value which will be inserted in the final string between each item of the sequence. A value of null is equivalent to the empty string.
Returns:
A String consisting of the XdmValue.asString() value of each item with the separator string inserted between each instance.
Throws:
RequestException - If an unrecoverable error occurs when submitting or evaluating the request.


getRequest

public Request getRequest()
Returns the Request object used internally to submit requests. This object can be used to set external variables that will be bound to the query when submitted. You should not set your own RequestOptions object, use setRequestOptions(com.marklogic.xcc.RequestOptions) instead.

Returns:
An instance of Request.


setRequestOptions

public void setRequestOptions(RequestOptions options)
Set (or clear) the RequestOptions instance to associate with submitted queries.

Parameters:
options - An instance of RequestOptions or null.


main

public static void main(String[] args)
                 throws Exception
Simple command-line driver that will read a query from a file and submit it to the server at the given URL. No validation of the query file is done to insure that it really is XQuery or that it's a reasonable size.

Parameters:
args - First arg: a URL as per ContentSourceFactory.newContentSource(java.net.URI). Second arg: a filename to read XQuery text from.
Throws:
Exception - No exceptions are handled, anything that goes wrong will spew out a stack trace and exit.


XCC/J 4.1-11

Copyright © 2003-2011 MarkLogic Corporation, All Rights Reserved.

Complete online documentation for MarkLogic Server, XQuery and related components may be found at developer.marklogic.com