public class SimpleQueryRunner
extends java.lang.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 and Description |
---|
SimpleQueryRunner(java.net.URI serverUri)
Construct an instance that will submit query
requests to the server represented by the given URI.
|
Modifier and Type | Method and Description |
---|---|
ResultSequence |
execute(java.lang.String query)
Submit the given query string and return a
ResultSequence
object. |
java.lang.String |
executeToSingleString(java.lang.String query,
java.lang.String separator)
Submit the given query string and return a
single String which is the concatenation of all the
ResultItem s,
separated by the given separator string. |
java.lang.String[] |
executeToStringArray(java.lang.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(java.lang.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. |
public SimpleQueryRunner(java.net.URI serverUri) throws XccConfigException
serverUri
- A URI that specifies a server per (
ContentSourceFactory.newContentSource(java.net.URI)
).XccConfigException
- If the URI is not a valid XCC server URL.public ResultSequence execute(java.lang.String query) throws RequestException
ResultSequence
object.query
- XQuery code as a String, to be evaluated
by the server.ResultSequence
,
possibly with size zero.RequestException
- If an unrecoverable error occurs when submitting or evaluating
the request.public java.lang.String[] executeToStringArray(java.lang.String query) throws RequestException
ResultItem
(see
XdmValue.asString()
)query
- XQuery code as a String, to be evaluated
by the server.ResultSequence
.RequestException
- If an unrecoverable error occurs when submitting or evaluating
the request.public java.lang.String executeToSingleString(java.lang.String query, java.lang.String separator) throws RequestException
ResultItem
s,
separated by the given separator string.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.
XdmValue.asString()
value of each item with the
separator string inserted between each instance.RequestException
- If an unrecoverable error occurs when submitting or evaluating
the request.public Request getRequest()
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.Request
.public void setRequestOptions(RequestOptions options)
RequestOptions
instance to associate with submitted queries.options
- An instance of RequestOptions
or
null.public static void main(java.lang.String[] args) throws java.lang.Exception
args
- First arg: a URL as per
ContentSourceFactory.newContentSource(java.net.URI)
.
Second arg: a filename to read XQuery text from.java.lang.Exception
- No exceptions are handled,
anything that goes wrong will spew out a stack trace and exit.
Copyright © 2023 MarkLogic
Corporation. All Rights Reserved.
Complete online documentation for MarkLogic Server,
XQuery and related components may
be found at
developer.marklogic.com