Search in sources :

Example 1 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 2 with OFunction

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

Example 3 with OFunction

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

Example 4 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 5 with OFunction

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);
}
Also used : ODocument(com.orientechnologies.orient.core.record.impl.ODocument) 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