ODatabaseRecordInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
if (db != null && !(db instanceof ODatabaseRecordTx))
db = db.getUnderlying();
final OScriptManager scriptManager = Orient.instance().getScriptManager();
CompiledScript compiledScript = request.getCompiledScript();
if (compiledScript == null) {
ScriptEngine scriptEngine = scriptManager.getEngine(language);
if (!(scriptEngine instanceof Compilable))
throw new OCommandExecutionException("Language '" + language + "' does not support compilation");
// COMPILE FUNCTION LIBRARY
String lib = scriptManager.getLibrary(db, language);
if (lib == null)
lib = "";
parserText = lib + parserText;
Compilable c = (Compilable) scriptEngine;
try {
compiledScript = c.compile(parserText);
} catch (ScriptException e) {
scriptManager.getErrorMessage(e, parserText);
}
request.setCompiledScript(compiledScript);
}
final Bindings binding = scriptManager.bind(compiledScript.getEngine().getBindings(ScriptContext.ENGINE_SCOPE),
(ODatabaseRecordTx) db, iContext, iArgs);
try {
final Object ob = compiledScript.eval(binding);
return OCommandExecutorUtility.transformResult(ob);
} catch (ScriptException e) {
throw new OCommandScriptException("Error on execution of the script", request.getText(), e.getColumnNumber(), e);