Search in sources :

Example 6 with OFunction

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();
}
Also used : OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) Test(org.testng.annotations.Test)

Example 7 with OFunction

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);
    }
}
Also used : ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) OPartitionedObjectPool(com.orientechnologies.common.concur.resource.OPartitionedObjectPool)

Example 8 with OFunction

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();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Example 9 with OFunction

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();
}
Also used : OScheduledEventBuilder(com.orientechnologies.orient.core.schedule.OScheduledEventBuilder) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Example 10 with OFunction

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;
}
Also used : ArrayList(java.util.ArrayList) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction)

Aggregations

OFunction (com.orientechnologies.orient.core.metadata.function.OFunction)18 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)3 Test (org.testng.annotations.Test)3 OException (com.orientechnologies.common.exception.OException)2 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)2 ArrayList (java.util.ArrayList)2 OPartitionedObjectPool (com.orientechnologies.common.concur.resource.OPartitionedObjectPool)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OFunctionLibrary (com.orientechnologies.orient.core.metadata.function.OFunctionLibrary)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)1