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 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 DistributedSchedulerTest method eventBySQL.
public void eventBySQL() throws Exception {
final ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(serverInstance.get(0)));
db.open("admin", "admin");
try {
OFunction func = createFunction(db);
// CREATE NEW EVENT
db.command(new OCommandSQL("insert into oschedule set name = 'test', function = ?, rule = \"0/1 * * * * ?\"")).execute(func.getId());
Thread.sleep(5000);
long count = getLogCounter(db);
Assert.assertTrue("count = " + count, count >= 4);
db.getLocalCache().invalidate();
OLogManager.instance().info(this, "UPDATING EVENT FROM 1 TO 2 SECONDS...");
// UPDATE
db.command(new OCommandSQL("update oschedule set rule = \"0/2 * * * * ?\" where name = 'test'")).execute(func.getId());
Thread.sleep(4000);
long newCount = getLogCounter(db);
Assert.assertTrue("newCount = " + newCount + " count=" + count, newCount - count > 1 && newCount - count <= 2);
// DELETE
db.command(new OCommandSQL("delete from oschedule where name = 'test'")).execute(func.getId());
Thread.sleep(3000);
count = newCount;
newCount = getLogCounter(db);
Assert.assertTrue("newCount = " + newCount, newCount - count <= 1);
} finally {
db.drop();
}
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class DistributedSchedulerTest method eventByAPI.
private void eventByAPI() throws InterruptedException {
final ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(serverInstance.get(0)));
db.open("admin", "admin");
OFunction func = createFunction(db);
db.getMetadata().getScheduler().scheduleEvent(new OScheduledEventBuilder().setName("test").setRule("0/1 * * * * ?").setFunction(func).build());
Thread.sleep(5000);
Long count = getLogCounter(db);
Assert.assertTrue("count = " + count, count > 0);
db.getMetadata().getScheduler().removeEvent("test");
db.close();
}
use of com.orientechnologies.orient.core.metadata.function.OFunction in project orientdb by orientechnologies.
the class DistributedSchedulerTest method createFunction.
private OFunction createFunction(final ODatabaseDocumentTx db) {
if (!db.getMetadata().getSchema().existsClass("scheduler_log"))
db.getMetadata().getSchema().createClass("scheduler_log");
OFunction func = db.getMetadata().getFunctionLibrary().getFunction("logEvent");
if (func == null) {
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;
}
Aggregations