Search in sources :

Example 11 with OBasicCommandContext

use of com.orientechnologies.orient.core.command.OBasicCommandContext in project orientdb by orientechnologies.

the class OServerCommandAbstractLogic method execute.

@Override
public boolean execute(final OHttpRequest iRequest, final OHttpResponse iResponse) throws Exception {
    final String[] parts = init(iRequest, iResponse);
    ODatabaseDocument db = null;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        final OFunction f = db.getMetadata().getFunctionLibrary().getFunction(parts[2]);
        if (f == null)
            throw new IllegalArgumentException("Function '" + parts[2] + "' is not configured");
        if (iRequest.httpMethod.equalsIgnoreCase("GET") && !f.isIdempotent()) {
            iResponse.send(OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, "GET method is not allowed to execute function '" + parts[2] + "' because has been declared as non idempotent. Use POST instead.", null);
            return false;
        }
        Object[] args = new String[parts.length - 3];
        for (int i = 3; i < parts.length; ++i) args[i - 3] = parts[i];
        // BIND CONTEXT VARIABLES
        final OBasicCommandContext context = new OBasicCommandContext();
        context.setVariable("session", OHttpSessionManager.getInstance().getSession(iRequest.sessionId));
        context.setVariable("request", new OHttpRequestWrapper(iRequest, (String[]) args));
        context.setVariable("response", new OHttpResponseWrapper(iResponse));
        final Object functionResult;
        if (args.length == 0 && iRequest.content != null && !iRequest.content.isEmpty()) {
            // PARSE PARAMETERS FROM CONTENT PAYLOAD
            try {
                final ODocument params = new ODocument().fromJSON(iRequest.content);
                functionResult = f.executeInContext(context, params.toMap());
            } catch (Exception e) {
                throw OException.wrapException(new OCommandScriptException("Error on parsing parameters from request body"), e);
            }
        } else
            functionResult = f.executeInContext(context, args);
        handleResult(iRequest, iResponse, functionResult);
    } catch (OCommandScriptException e) {
        // EXCEPTION
        final StringBuilder msg = new StringBuilder(256);
        for (Exception currentException = e; currentException != null; currentException = (Exception) currentException.getCause()) {
            if (msg.length() > 0)
                msg.append("\n");
            msg.append(currentException.getMessage());
        }
        if (isJsonResponse(iResponse)) {
            sendJsonError(iResponse, OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, msg.toString(), null);
        } else {
            iResponse.send(OHttpUtils.STATUS_BADREQ_CODE, OHttpUtils.STATUS_BADREQ_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, msg.toString(), null);
        }
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : OCommandScriptException(com.orientechnologies.orient.core.command.script.OCommandScriptException) OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) OHttpResponseWrapper(com.orientechnologies.orient.server.network.protocol.http.OHttpResponseWrapper) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OCommandScriptException(com.orientechnologies.orient.core.command.script.OCommandScriptException) OFunction(com.orientechnologies.orient.core.metadata.function.OFunction) OHttpRequestWrapper(com.orientechnologies.orient.server.network.protocol.http.OHttpRequestWrapper) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with OBasicCommandContext

use of com.orientechnologies.orient.core.command.OBasicCommandContext in project orientdb by orientechnologies.

the class OETLProcessor method createDefaultContext.

protected static OCommandContext createDefaultContext() {
    final OCommandContext context = new OBasicCommandContext();
    context.setVariable("dumpEveryMs", 1000);
    return context;
}
Also used : OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) OCommandContext(com.orientechnologies.orient.core.command.OCommandContext)

Example 13 with OBasicCommandContext

use of com.orientechnologies.orient.core.command.OBasicCommandContext in project orientdb by orientechnologies.

the class DocumentTest method testEvalInContext.

@Test
public void testEvalInContext() {
    ODocument doc = new ODocument();
    doc.field("amount", 300);
    OCommandContext context = new OBasicCommandContext().setVariable("vat", 20);
    Number amountPlusVat = (Number) doc.eval("amount * (100 + $vat) / 100", context);
    Assert.assertEquals(amountPlusVat.longValue(), 360l);
}
Also used : OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) OCommandContext(com.orientechnologies.orient.core.command.OCommandContext) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Example 14 with OBasicCommandContext

use of com.orientechnologies.orient.core.command.OBasicCommandContext in project orientdb by orientechnologies.

the class SQLFunctionDifferenceTest method testExecute.

@Test
public void testExecute() {
    final OSQLFunctionDifference function = new OSQLFunctionDifference();
    List<List<Object>> incomes = Arrays.asList(Arrays.<Object>asList(1, 2, 3, 4, 5, 1), Arrays.<Object>asList(3, 5, 6, 7, 0, 1, 3, 3, 6), Arrays.<Object>asList(2, 2, 8, 9));
    Set<Object> expectedResult = new HashSet<Object>(Arrays.<Object>asList(4));
    Set<Object> actualResult = (Set<Object>) function.execute(null, null, null, incomes.toArray(), new OBasicCommandContext());
    assertSetEquals(actualResult, expectedResult);
    incomes = Arrays.asList(Arrays.<Object>asList(1, 2, 3, 4, 5, 1), Arrays.<Object>asList(3, 5, 6, 7, 0, 1, 3, 3, 6));
    expectedResult = new HashSet<Object>(Arrays.<Object>asList(2, 4));
    actualResult = (Set<Object>) function.execute(null, null, null, incomes.toArray(), new OBasicCommandContext());
    assertSetEquals(actualResult, expectedResult);
}
Also used : OBasicCommandContext(com.orientechnologies.orient.core.command.OBasicCommandContext) Test(org.testng.annotations.Test)

Example 15 with OBasicCommandContext

use of com.orientechnologies.orient.core.command.OBasicCommandContext 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)

Aggregations

OBasicCommandContext (com.orientechnologies.orient.core.command.OBasicCommandContext)29 Test (org.junit.Test)22 HashMap (java.util.HashMap)16 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)13 ORID (com.orientechnologies.orient.core.id.ORID)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 Test (org.testng.annotations.Test)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)3 OFunction (com.orientechnologies.orient.core.metadata.function.OFunction)3 OCommandContext (com.orientechnologies.orient.core.command.OCommandContext)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OException (com.orientechnologies.common.exception.OException)1 OPair (com.orientechnologies.common.util.OPair)1 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)1 ODocumentComparator (com.orientechnologies.orient.core.record.impl.ODocumentComparator)1 OETLBaseTest (com.orientechnologies.orient.etl.OETLBaseTest)1 OHttpRequestWrapper (com.orientechnologies.orient.server.network.protocol.http.OHttpRequestWrapper)1 OHttpResponseWrapper (com.orientechnologies.orient.server.network.protocol.http.OHttpResponseWrapper)1 IOException (java.io.IOException)1