Search in sources :

Example 11 with OSQLFilterItemField

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

the class OCommandExecutorSQLResultsetAbstract method optimizeBranch.

protected void optimizeBranch(final OSQLFilterCondition iParentCondition, OSQLFilterCondition iCondition) {
    if (iCondition == null)
        return;
    Object left = iCondition.getLeft();
    if (left instanceof OSQLFilterCondition) {
        // ANALYSE LEFT RECURSIVELY
        optimizeBranch(iCondition, (OSQLFilterCondition) left);
    } else if (left instanceof OSQLFunctionRuntime) {
        left = optimizeFunction((OSQLFunctionRuntime) left);
        iCondition.setLeft(left);
    }
    Object right = iCondition.getRight();
    if (right instanceof OSQLFilterCondition) {
        // ANALYSE RIGHT RECURSIVELY
        optimizeBranch(iCondition, (OSQLFilterCondition) right);
    } else if (right instanceof OSQLFunctionRuntime) {
        right = optimizeFunction((OSQLFunctionRuntime) right);
        iCondition.setRight(right);
    }
    final OQueryOperator oper = iCondition.getOperator();
    Object result = null;
    if (left instanceof OSQLFilterItemField && right instanceof OSQLFilterItemField) {
        if (((OSQLFilterItemField) left).getRoot().equals(((OSQLFilterItemField) right).getRoot())) {
            if (oper instanceof OQueryOperatorEquals)
                result = Boolean.TRUE;
            else if ((oper instanceof OQueryOperatorNotEquals) || (oper instanceof OQueryOperatorNotEquals2))
                result = Boolean.FALSE;
        }
    }
    if (result != null) {
        if (iParentCondition != null)
            if (iCondition == iParentCondition.getLeft())
                // REPLACE LEFT
                iCondition.setLeft(result);
            else
                // REPLACE RIGHT
                iCondition.setRight(result);
        else {
            // REPLACE ROOT CONDITION
            if (result instanceof Boolean && ((Boolean) result))
                compiledFilter.setRootCondition(null);
        }
    }
}
Also used : OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) OQueryOperatorNotEquals(com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals) OQueryOperatorNotEquals2(com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals2) OQueryOperator(com.orientechnologies.orient.core.sql.operator.OQueryOperator) OQueryOperatorEquals(com.orientechnologies.orient.core.sql.operator.OQueryOperatorEquals)

Example 12 with OSQLFilterItemField

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

the class OQueryOperatorContains 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;
    if (iLeft instanceof Iterable<?>) {
        final Iterable<Object> iterable = (Iterable<Object>) iLeft;
        if (condition != null) {
            // CHECK AGAINST A CONDITION
            for (final Object o : iterable) {
                final OIdentifiable id;
                if (o instanceof OIdentifiable)
                    id = (OIdentifiable) o;
                else if (o instanceof Map<?, ?>) {
                    final Iterator<Object> iter = ((Map<?, Object>) o).values().iterator();
                    final Object v = iter.hasNext() ? iter.next() : null;
                    if (v instanceof OIdentifiable)
                        id = (OIdentifiable) v;
                    else
                        // TRANSFORM THE ENTIRE MAP IN A DOCUMENT. PROBABLY HAS BEEN IMPORTED FROM JSON
                        id = new ODocument((Map) o);
                } else if (o instanceof Iterable<?>) {
                    final Iterator<OIdentifiable> iter = ((Iterable<OIdentifiable>) o).iterator();
                    id = iter.hasNext() ? iter.next() : null;
                } else
                    continue;
                if ((Boolean) condition.evaluate(id, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            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();
                        }
                    }
                }
            }
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iRight, o, type))
                    return true;
            }
        }
    } else if (iRight instanceof Iterable<?>) {
        // CHECK AGAINST A CONDITION
        final Iterable<OIdentifiable> iterable = (Iterable<OIdentifiable>) iRight;
        if (condition != null) {
            for (final OIdentifiable o : iterable) {
                if ((Boolean) condition.evaluate(o, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iLeft, o))
                    return true;
            }
        }
    }
    return false;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) Iterator(java.util.Iterator) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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