use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class OCommandExecutorFunction method executeInContext.
public Object executeInContext(final OCommandContext iContext, final Map<Object, Object> iArgs) {
parserText = request.getText();
ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(parserText);
db.checkSecurity(ORule.ResourceGeneric.FUNCTION, ORole.PERMISSION_READ, f.getName());
final OScriptManager scriptManager = Orient.instance().getScriptManager();
final OPartitionedObjectPool.PoolEntry<ScriptEngine> entry = scriptManager.acquireDatabaseEngine(db.getName(), f.getLanguage());
final ScriptEngine scriptEngine = entry.object;
try {
final Bindings binding = scriptManager.bind(scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE), (ODatabaseDocumentTx) db, iContext, iArgs);
try {
final Object result;
if (scriptEngine instanceof Invocable) {
// INVOKE AS FUNCTION. PARAMS ARE PASSED BY POSITION
final Invocable invocableEngine = (Invocable) scriptEngine;
Object[] args = null;
if (iArgs != null) {
args = new Object[iArgs.size()];
int i = 0;
for (Entry<Object, Object> arg : iArgs.entrySet()) args[i++] = arg.getValue();
} else {
args = OCommonConst.EMPTY_OBJECT_ARRAY;
}
result = invocableEngine.invokeFunction(parserText, args);
} else {
// INVOKE THE CODE SNIPPET
final Object[] args = iArgs == null ? null : iArgs.values().toArray();
result = scriptEngine.eval(scriptManager.getFunctionInvoke(f, args), binding);
}
return OCommandExecutorUtility.transformResult(result);
} catch (ScriptException e) {
throw OException.wrapException(new OCommandScriptException("Error on execution of the script", request.getText(), e.getColumnNumber()), e);
} catch (NoSuchMethodException e) {
throw OException.wrapException(new OCommandScriptException("Error on execution of the script", request.getText(), 0), e);
} catch (OCommandScriptException e) {
// PASS THROUGH
throw e;
} finally {
scriptManager.unbind(binding, iContext, iArgs);
}
} finally {
scriptManager.releaseDatabaseEngine(f.getLanguage(), db.getName(), entry);
}
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class OSchedulerTest method createFunction.
private OFunction createFunction(ODatabaseDocumentTx db) {
db.getMetadata().getSchema().createClass("scheduler_log");
OFunction func = db.getMetadata().getFunctionLibrary().createFunction("logEvent");
func.setLanguage("SQL");
func.setCode("insert into scheduler_log set timestamp = sysdate(), note = :note");
final List<String> pars = new ArrayList<String>();
pars.add("note");
func.setParameters(pars);
func.save();
return func;
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class OSchedulerTest method createLogEvent.
private void createLogEvent(ODatabaseDocumentTx db) {
OFunction func = createFunction(db);
Map<Object, Object> args = new HashMap<Object, Object>();
args.put("note", "test");
db.getMetadata().getScheduler().scheduleEvent(new OScheduledEventBuilder().setName("test").setRule("0/1 * * * * ?").setFunction(func).setArguments(args).build());
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class OFunctionSqlTest method functionSqlWithParameters.
@Test
public void functionSqlWithParameters() {
ODatabaseDocument db = new ODatabaseDocumentTx("memory:functionSqlWithParameters");
db.create();
// ODatabaseRecordThreadLocal.INSTANCE.set(db);
ODocument doc1 = new ODocument("Test");
doc1.field("name", "Enrico");
db.save(doc1);
doc1.reset();
doc1.setClassName("Test");
doc1.field("name", "Luca");
db.save(doc1);
OFunction function = new OFunction();
function.setName("test");
function.setCode("select from Test where name = :name");
function.setParameters(new ArrayList<String>() {
{
add("name");
}
});
function.save();
Object result = function.executeInContext(new OBasicCommandContext(), "Enrico");
System.out.println(result);
Assert.assertEquals(((OResultSet) result).size(), 1);
db.drop();
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class OrientJdbcDatabaseMetaData method getFunctionColumns.
public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException {
database.activateOnCurrentThread();
final List<ODocument> records = new ArrayList<ODocument>();
final OFunction f = database.getMetadata().getFunctionLibrary().getFunction(functionNamePattern);
for (String p : f.getParameters()) {
final ODocument doc = new ODocument().field("FUNCTION_CAT", (Object) null).field("FUNCTION_SCHEM", (Object) null).field("FUNCTION_NAME", f.getName()).field("COLUMN_NAME", p).field("COLUMN_TYPE", procedureColumnIn).field("DATA_TYPE", java.sql.Types.OTHER).field("SPECIFIC_NAME", f.getName());
records.add(doc);
}
final ODocument doc = new ODocument().field("FUNCTION_CAT", (Object) null).field("FUNCTION_SCHEM", (Object) null).field("FUNCTION_NAME", f.getName()).field("COLUMN_NAME", "return").field("COLUMN_TYPE", procedureColumnReturn).field("DATA_TYPE", java.sql.Types.OTHER).field("SPECIFIC_NAME", f.getName());
records.add(doc);
return new OrientJdbcResultSet(new OrientJdbcStatement(connection), records, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
Aggregations