Interface ServerEvaluationCall


public interface ServerEvaluationCall

ServerEvaluationCall uses a fluent builder-style API to collect the parameters for a server-side xquery or javascript eval or invoke (modulePath) call. ServerEvaluationCall also conveniently has the eval* methods which execute those calls and return the results. You must call one and only one of the following methods: xquery, javascript, or modulePath. The xquery and javascript methods initialize this call for server-side eval and accept source code as a String or a TextWriteHandle (in case you are streaming the source code from the file system, a URL, or other source that is most easily accessed via io handles). The modulePath method initializes this call for server- side invoke given the path to a module previously installed on the server.

Here is a simple "hello world" junit example:

    String javascript = "'hello world'";
    String response = client.newServerEval()
        .javascript(javascript)
        .evalAs(String.class);
    assertEquals("hello world", response);

or in xquery:

    String xquery = "'hello world'";
    String response = client.newServerEval()
        .xquery(xquery)
        .evalAs(String.class);
    assertEquals("hello world", response);

Variables can be added with the addVariable methods. addVariable(String, AbstractWriteHandle) allows you to pass complex JSON or XML values directly from io handles. addVariableAs(String, Object) follows the shortcut pattern which maps objects by type to the appropriate handle. For simpler atomic values, convenience addVariable methods are provided for String, Number, and Boolean types.

Here is a simple "hello solar system" example with a variable:

    String javascript = "var planet;'hello solar system from ' + planet";
    String response = client.newServerEval()
        .javascript(javascript)
        .addVariable("planet", "Mars")
        .evalAs(String.class);
    assertEquals( "hello solar system from Mars", response);

or in xquery:

    String xquery = "declare variable $planet external;'hello solar system from ' || $planet";
    String response = client.newServerEval()
        .xquery(xquery)
        .addVariable("planet", "Mars")
        .evalAs(String.class);
    assertEquals( "hello solar system from Mars", response);
 

Each call can be executed within a transaction, within a particular database, and with particular namespaces available for expansion of prefixed variable names.

Each call can be executed with only one expected response of a particular type or handle type. Or calls can be executed with multiple responses expected. Calls that expect only one response but need to stream the response should still use eval() and EvalResultIterator so the response isn't closed before the streaming begins.

NOTE: EvalResultIterator MUST BE CLOSED. If you call eval() don't forget to call close() on the returned EvalResultIterator to free up the underlying resources.