Skip to main content

8.5 JSR-223

dataql-engine implements the JDK javax.script APIs. Discover it through ScriptEngineManager, execute scripts, or precompile them with Compilable.

Execute a script​

Jsr223Example.java
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import net.hasor.dataql.kernel.QueryResult;

public class Jsr223Example {
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("dataql");
if (engine == null) {
throw new IllegalStateException("dataql-engine is missing from the runtime classpath");
}
engine.put("name", "DataQL");
QueryResult result = (QueryResult) engine.eval("return ${name};");
System.out.println(result.getData().unwrap());
}
}

The output is DataQL. eval returns a QueryResult; read its value through getData().unwrap(). Discovery also supports extension dql and MIME type application/x-dataql.

Precompile and supply per-call parameters​

Continue with the engine above. Compile once and execute twice using separate contexts:

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import net.hasor.dataql.host.jsr223.QueryScriptContext;
import net.hasor.dataql.kernel.QueryResult;

CompiledScript compiled = ((Compilable) engine).compile("return ${name};");
QueryScriptContext defaults = (QueryScriptContext) engine.getContext();

for (String name : new String[] {"Alice", "Bob"}) {
QueryScriptContext context = new QueryScriptContext(defaults.getHostContext());
context.setAttribute("name", name, ScriptContext.ENGINE_SCOPE);
QueryResult result = (QueryResult) compiled.eval(context);
System.out.println(result.getData().unwrap());
}

The output is Alice, then Bob. Contexts must be QueryScriptContext instances sharing the engine's HostContext. Plain SimpleScriptContext, eval(script, bindings) and compiled.eval(bindings) are currently unsupported. Use the explicit context pattern above for per-call values.

Parameters and configuration​

  • engine.put(...) writes to ENGINE_SCOPE. Parameter lookup merges GLOBAL_SCOPE first and then ENGINE_SCOPE, whose matching values win. $, @ and # read the same merged values.
  • GLOBAL_SCOPE keys also declare shared variables that scripts can reference by name. Set these keys before compilation. New contexts must be supplied with any required global Bindings.
  • Retrieve HostConfiguration through ((QueryScriptContext) engine.getContext()).getConfiguration() and use core extension registration.
  • The engine implements Compilable, but not Invocable. Runtime QueryRuntimeException failures are wrapped in ScriptException; parsing or compilation errors may propagate directly.