Search in sources :

Example 1 with OCommandExecutorNotFoundException

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

the class OSQLFunctionRuntime method execute.

/**
 * Execute a function.
 *
 * @param iCurrentRecord
 *          Current record
 * @param iCurrentResult
 *          TODO
 * @param iContext
 * @return
 */
public Object execute(final Object iThis, final OIdentifiable iCurrentRecord, final Object iCurrentResult, final OCommandContext iContext) {
    // RESOLVE VALUES USING THE CURRENT RECORD
    for (int i = 0; i < configuredParameters.length; ++i) {
        runtimeParameters[i] = configuredParameters[i];
        if (configuredParameters[i] instanceof OSQLFilterItemField) {
            runtimeParameters[i] = ((OSQLFilterItemField) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
        } else if (configuredParameters[i] instanceof OSQLFunctionRuntime)
            runtimeParameters[i] = ((OSQLFunctionRuntime) configuredParameters[i]).execute(iThis, iCurrentRecord, iCurrentResult, iContext);
        else if (configuredParameters[i] instanceof OSQLFilterItemVariable) {
            runtimeParameters[i] = ((OSQLFilterItemVariable) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
        } else if (configuredParameters[i] instanceof OCommandSQL) {
            try {
                runtimeParameters[i] = ((OCommandSQL) configuredParameters[i]).setContext(iContext).execute();
            } catch (OCommandExecutorNotFoundException e) {
                // TRY WITH SIMPLE CONDITION
                final String text = ((OCommandSQL) configuredParameters[i]).getText();
                final OSQLPredicate pred = new OSQLPredicate(text);
                runtimeParameters[i] = pred.evaluate(iCurrentRecord instanceof ORecord ? (ORecord) iCurrentRecord : null, (ODocument) iCurrentResult, iContext);
                // REPLACE ORIGINAL PARAM
                configuredParameters[i] = pred;
            }
        } else if (configuredParameters[i] instanceof OSQLPredicate)
            runtimeParameters[i] = ((OSQLPredicate) configuredParameters[i]).evaluate(iCurrentRecord.getRecord(), (iCurrentRecord instanceof ODocument ? (ODocument) iCurrentResult : null), iContext);
        else if (configuredParameters[i] instanceof String) {
            if (configuredParameters[i].toString().startsWith("\"") || configuredParameters[i].toString().startsWith("'"))
                runtimeParameters[i] = OIOUtils.getStringContent(configuredParameters[i]);
        }
    }
    if (function.getMaxParams() == -1 || function.getMaxParams() > 0) {
        if (runtimeParameters.length < function.getMinParams() || (function.getMaxParams() > -1 && runtimeParameters.length > function.getMaxParams()))
            throw new OCommandExecutionException("Syntax error: function '" + function.getName() + "' needs " + (function.getMinParams() == function.getMaxParams() ? function.getMinParams() : function.getMinParams() + "-" + function.getMaxParams()) + " argument(s) while has been received " + runtimeParameters.length);
    }
    final Object functionResult = function.execute(iThis, iCurrentRecord, iCurrentResult, runtimeParameters, iContext);
    if (functionResult instanceof OAutoConvertToRecord)
        // FORCE AVOIDING TO CONVERT IN RECORD
        ((OAutoConvertToRecord) functionResult).setAutoConvertToRecord(false);
    return transformValue(iCurrentRecord, iContext, functionResult);
}
Also used : OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable) OCommandExecutorNotFoundException(com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoConvertToRecord(com.orientechnologies.orient.core.db.record.OAutoConvertToRecord) OSQLPredicate(com.orientechnologies.orient.core.sql.filter.OSQLPredicate) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) ORecord(com.orientechnologies.orient.core.record.ORecord) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with OCommandExecutorNotFoundException

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

the class OCommandExecutorSQLDelegate method parse.

@SuppressWarnings("unchecked")
public OCommandExecutorSQLDelegate parse(final OCommandRequest iCommand) {
    if (iCommand instanceof OCommandRequestText) {
        final OCommandRequestText textRequest = (OCommandRequestText) iCommand;
        final String text = textRequest.getText();
        if (text == null)
            throw new IllegalArgumentException("Command text is null");
        final String textUpperCase = upperCase(text);
        delegate = OSQLEngine.getInstance().getCommand(textUpperCase);
        if (delegate == null)
            throw new OCommandExecutorNotFoundException("Cannot find a command executor for the command request: " + iCommand);
        delegate.setContext(context);
        delegate.setLimit(iCommand.getLimit());
        delegate.parse(iCommand);
        delegate.setProgressListener(progressListener);
        if (delegate.getFetchPlan() != null)
            textRequest.setFetchPlan(delegate.getFetchPlan());
    } else
        throw new OCommandExecutionException("Cannot find a command executor for the command request: " + iCommand);
    return this;
}
Also used : OCommandRequestText(com.orientechnologies.orient.core.command.OCommandRequestText) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OCommandExecutorNotFoundException(com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException)

Aggregations

OCommandExecutorNotFoundException (com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)1 OAutoConvertToRecord (com.orientechnologies.orient.core.db.record.OAutoConvertToRecord)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)1 OSQLFilterItemVariable (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)1 OSQLPredicate (com.orientechnologies.orient.core.sql.filter.OSQLPredicate)1