Support new SQL executor for functions with language=sql
Resolves:
package com.orientechnologies.orient.core.sql;
import com.orientechnologies.orient.core.command.script.OCommandScript;
-
-import javax.script.Bindings;
-import javax.script.ScriptContext;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineFactory;
-import javax.script.ScriptException;
-import javax.script.SimpleBindings;
+import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
+import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
+import com.orientechnologies.orient.core.db.record.OIdentifiable;
+import com.orientechnologies.orient.core.exception.OCommandExecutionException;
+import com.orientechnologies.orient.core.sql.executor.OResultSet;
+import com.orientechnologies.orient.core.sql.query.OBasicLegacyResultSet;
+import com.orientechnologies.orient.core.sql.query.OLegacyResultSet;
+
+import javax.script.*;
import java.io.IOException;
import java.io.Reader;
+import java.util.HashMap;
+import java.util.Map;
/**
* Dynamic script engine for OrientDB SQL commands. This implementation is multi-threads.
*
* @author Luca Garulli (l.garulli--(at)--orientdb.com)
- *
*/
public class OSQLScriptEngine implements ScriptEngine {
@@ -68,7 +71,39 @@ public class OSQLScriptEngine implements ScriptEngine {
@Override
public Object eval(String script, Bindings n) throws ScriptException {
- return new OCommandScript(script).execute(n);
+ ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.instance().getIfDefined();
+ if (db == null) {
+ throw new OCommandExecutionException("No database available in threadlocal");
+ }
+ OResultSet queryResult = db.command(script, convertToParameters(n));
+ OLegacyResultSet finalResult = new OBasicLegacyResultSet();
+ queryResult.stream().forEach(x -> finalResult.add(x));
+ queryResult.close();
+ return finalResult;
+ }
+
+ @SuppressWarnings("unchecked")
+ protected Map<Object, Object> convertToParameters(Object... iArgs) {
+ final Map<Object, Object> params;
+
+ if (iArgs.length == 1 && iArgs[0] instanceof Map) {
+ params = (Map<Object, Object>) iArgs[0];
+ } else {
+ if (iArgs.length == 1 && iArgs[0] != null && iArgs[0].getClass().isArray() && iArgs[0] instanceof Object[])
+ iArgs = (Object[]) iArgs[0];
+
+ params = new HashMap<Object, Object>(iArgs.length);
+ for (int i = 0; i < iArgs.length; ++i) {
+ Object par = iArgs[i];
+
+ if (par instanceof OIdentifiable && ((OIdentifiable) par).getIdentity().isValid())
+ // USE THE RID ONLY
+ par = ((OIdentifiable) par).getIdentity();
+
+ params.put(i, par);
+ }
+ }
+ return params;
}
@Override
