Search in sources :

Example 1 with OSQLFilterItemField

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField 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 OSQLFilterItemField

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.

the class OQueryOperatorContainsValue method evaluateExpression.

@Override
@SuppressWarnings("unchecked")
protected boolean evaluateExpression(final OIdentifiable iRecord, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight, OCommandContext iContext) {
    final OSQLFilterCondition condition;
    if (iCondition.getLeft() instanceof OSQLFilterCondition)
        condition = (OSQLFilterCondition) iCondition.getLeft();
    else if (iCondition.getRight() instanceof OSQLFilterCondition)
        condition = (OSQLFilterCondition) iCondition.getRight();
    else
        condition = null;
    OType type = null;
    if (iCondition.getLeft() instanceof OSQLFilterItemField && ((OSQLFilterItemField) iCondition.getLeft()).isFieldChain() && ((OSQLFilterItemField) iCondition.getLeft()).getFieldChain().getItemCount() == 1) {
        String fieldName = ((OSQLFilterItemField) iCondition.getLeft()).getFieldChain().getItemName(0);
        if (fieldName != null) {
            Object record = iRecord.getRecord();
            if (record instanceof ODocument) {
                OProperty property = ((ODocument) record).getSchemaClass().getProperty(fieldName);
                if (property != null && property.getType().isMultiValue()) {
                    type = property.getLinkedType();
                }
            }
        }
    }
    Object right = iRight;
    if (type != null) {
        right = OType.convert(iRight, type.getDefaultJavaType());
    }
    if (iLeft instanceof Map<?, ?>) {
        final Map<String, ?> map = (Map<String, ?>) iLeft;
        if (condition != null) {
            // CHECK AGAINST A CONDITION
            for (Object o : map.values()) {
                o = loadIfNeed(o);
                if ((Boolean) condition.evaluate((ODocument) o, null, iContext))
                    return true;
            }
        } else
            return map.containsValue(right);
    } else if (iRight instanceof Map<?, ?>) {
        final Map<String, ?> map = (Map<String, ?>) iRight;
        if (condition != null)
            // CHECK AGAINST A CONDITION
            for (Object o : map.values()) {
                o = loadIfNeed(o);
                if ((Boolean) condition.evaluate((ODocument) o, null, iContext))
                    return true;
                else
                    return map.containsValue(iLeft);
            }
    }
    return false;
}
Also used : OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OType(com.orientechnologies.orient.core.metadata.schema.OType) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OSQLFilterItemField

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.

the class OQueryOperatorContainsText method filterRecords.

@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public Collection<OIdentifiable> filterRecords(final ODatabase<?> iDatabase, final List<String> iTargetClasses, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight) {
    final String fieldName;
    if (iCondition.getLeft() instanceof OSQLFilterItemField)
        fieldName = iCondition.getLeft().toString();
    else
        fieldName = iCondition.getRight().toString();
    final String fieldValue;
    if (iCondition.getLeft() instanceof OSQLFilterItemField)
        fieldValue = iCondition.getRight().toString();
    else
        fieldValue = iCondition.getLeft().toString();
    final String className = iTargetClasses.get(0);
    final OProperty prop = ((OMetadataInternal) iDatabase.getMetadata()).getImmutableSchemaSnapshot().getClass(className).getProperty(fieldName);
    if (prop == null)
        // NO PROPERTY DEFINED
        return null;
    OIndex<?> fullTextIndex = null;
    for (final OIndex<?> indexDefinition : prop.getIndexes()) {
        if (indexDefinition instanceof OIndexFullText) {
            fullTextIndex = indexDefinition;
            break;
        }
    }
    if (fullTextIndex == null) {
        return null;
    }
    return (Collection<OIdentifiable>) fullTextIndex.get(fieldValue);
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OMetadataInternal(com.orientechnologies.orient.core.metadata.OMetadataInternal) OIndexFullText(com.orientechnologies.orient.core.index.OIndexFullText) Collection(java.util.Collection)

Example 4 with OSQLFilterItemField

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.

the class OFilterAnalyzer method createIndexedProperty.

/**
 * Add SQL filter field to the search candidate list.
 *
 * @param iCondition Condition item
 * @param iItem      Value to search
 * @return true if the property was indexed and found, otherwise false
 */
private OIndexSearchResult createIndexedProperty(final OSQLFilterCondition iCondition, final Object iItem, OCommandContext ctx) {
    if (iItem == null || !(iItem instanceof OSQLFilterItemField)) {
        return null;
    }
    if (iCondition.getLeft() instanceof OSQLFilterItemField && iCondition.getRight() instanceof OSQLFilterItemField) {
        return null;
    }
    final OSQLFilterItemField item = (OSQLFilterItemField) iItem;
    if (item.hasChainOperators() && !item.isFieldChain()) {
        return null;
    }
    final Object origValue = iCondition.getLeft() == iItem ? iCondition.getRight() : iCondition.getLeft();
    OQueryOperator operator = iCondition.getOperator();
    if (iCondition.getRight() == iItem) {
        if (operator instanceof OQueryOperatorIn) {
            operator = new OQueryOperatorContains();
        } else if (operator instanceof OQueryOperatorContains) {
            operator = new OQueryOperatorIn();
        }
    }
    if (iCondition.getOperator() instanceof OQueryOperatorBetween || operator instanceof OQueryOperatorIn) {
        return new OIndexSearchResult(operator, item.getFieldChain(), origValue);
    }
    final Object value = OSQLHelper.getValue(origValue, null, ctx);
    return new OIndexSearchResult(operator, item.getFieldChain(), value);
}
Also used : OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)

Example 5 with OSQLFilterItemField

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.

the class OLuceneOperatorUtil method buildOIndexSearchResult.

public static OIndexSearchResult buildOIndexSearchResult(OClass iSchemaClass, OSQLFilterCondition iCondition, List<OIndexSearchResult> iIndexSearchResults, OCommandContext context) {
    if (iCondition.getLeft() instanceof Collection) {
        OIndexSearchResult lastResult = null;
        Collection left = (Collection) iCondition.getLeft();
        int i = 0;
        Object lastValue = null;
        for (Object obj : left) {
            if (obj instanceof OSQLFilterItemField) {
                OSQLFilterItemField item = (OSQLFilterItemField) obj;
                Object value = null;
                if (iCondition.getRight() instanceof Collection) {
                    List<Object> right = (List<Object>) iCondition.getRight();
                    value = right.get(i);
                } else {
                    value = iCondition.getRight();
                }
                if (lastResult == null) {
                    lastResult = new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value);
                } else {
                    lastResult = lastResult.merge(new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value));
                }
            } else if (obj instanceof OSQLFilterItemVariable) {
                OSQLFilterItemVariable item = (OSQLFilterItemVariable) obj;
                Object value = null;
                if (iCondition.getRight() instanceof Collection) {
                    List<Object> right = (List<Object>) iCondition.getRight();
                    value = right.get(i);
                } else {
                    value = iCondition.getRight();
                }
                context.setVariable(item.toString(), value);
            }
            i++;
        }
        if (lastResult != null && OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, lastResult))
            iIndexSearchResults.add(lastResult);
        return lastResult;
    } else {
        OIndexSearchResult result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getLeft());
        if (result == null)
            result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getRight());
        if (result == null)
            return null;
        if (OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, result))
            iIndexSearchResults.add(result);
        return result;
    }
}
Also used : OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) Collection(java.util.Collection) List(java.util.List) OIndexSearchResult(com.orientechnologies.orient.core.sql.OIndexSearchResult) OSQLFilterItemVariable(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)

Aggregations

OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)12 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)3 OType (com.orientechnologies.orient.core.metadata.schema.OType)3 OSQLFilterCondition (com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition)3 OSQLFilterItemVariable (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OIndexSearchResult (com.orientechnologies.orient.core.sql.OIndexSearchResult)2 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)2 Collection (java.util.Collection)2 Map (java.util.Map)2 OPair (com.orientechnologies.common.util.OPair)1 OResettable (com.orientechnologies.common.util.OResettable)1 OLuceneFullTextIndex (com.orientechnologies.lucene.index.OLuceneFullTextIndex)1 OCommandExecutorNotFoundException (com.orientechnologies.orient.core.command.OCommandExecutorNotFoundException)1 OAutoConvertToRecord (com.orientechnologies.orient.core.db.record.OAutoConvertToRecord)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORID (com.orientechnologies.orient.core.id.ORID)1