Search in sources :

Example 1 with OQueryRuntimeValueMulti

use of com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti in project orientdb by orientechnologies.

the class OQueryOperatorEquality method evaluateRecord.

@Override
public Object evaluateRecord(final OIdentifiable iRecord, ODocument iCurrentResult, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight, OCommandContext iContext) {
    if (iLeft instanceof OBinaryField && iRight instanceof OBinaryField)
        // BINARY COMPARISON
        return evaluate((OBinaryField) iLeft, (OBinaryField) iRight, iContext);
    else if (iLeft instanceof OQueryRuntimeValueMulti) {
        // LEFT = MULTI
        final OQueryRuntimeValueMulti left = (OQueryRuntimeValueMulti) iLeft;
        if (left.getValues().length == 0)
            return false;
        if (left.getDefinition().getRoot().startsWith(OSQLFilterItemFieldAll.NAME)) {
            // ALL VALUES
            for (int i = 0; i < left.getValues().length; ++i) {
                Object v = left.getValues()[i];
                Object r = iRight;
                final OCollate collate = left.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    r = collate.transform(iRight);
                }
                if (v == null || !evaluateExpression(iRecord, iCondition, v, r, iContext))
                    return false;
            }
            return true;
        } else {
            // ANY VALUES
            for (int i = 0; i < left.getValues().length; ++i) {
                Object v = left.getValues()[i];
                Object r = iRight;
                final OCollate collate = left.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    r = collate.transform(iRight);
                }
                if (v != null && evaluateExpression(iRecord, iCondition, v, r, iContext))
                    return true;
            }
            return false;
        }
    } else if (iRight instanceof OQueryRuntimeValueMulti) {
        // RIGHT = MULTI
        final OQueryRuntimeValueMulti right = (OQueryRuntimeValueMulti) iRight;
        if (right.getValues().length == 0)
            return false;
        if (right.getDefinition().getRoot().startsWith(OSQLFilterItemFieldAll.NAME)) {
            // ALL VALUES
            for (int i = 0; i < right.getValues().length; ++i) {
                Object v = right.getValues()[i];
                Object l = iLeft;
                final OCollate collate = right.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    l = collate.transform(iLeft);
                }
                if (v == null || !evaluateExpression(iRecord, iCondition, l, v, iContext))
                    return false;
            }
            return true;
        } else {
            // ANY VALUES
            for (int i = 0; i < right.getValues().length; ++i) {
                Object v = right.getValues()[i];
                Object l = iLeft;
                final OCollate collate = right.getCollate(i);
                if (collate != null) {
                    v = collate.transform(v);
                    l = collate.transform(iLeft);
                }
                if (v != null && evaluateExpression(iRecord, iCondition, l, v, iContext))
                    return true;
            }
            return false;
        }
    } else {
        // SINGLE SIMPLE ITEM
        return evaluateExpression(iRecord, iCondition, iLeft, iRight, iContext);
    }
}
Also used : OBinaryField(com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField) OCollate(com.orientechnologies.orient.core.collate.OCollate) OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti)

Example 2 with OQueryRuntimeValueMulti

use of com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti in project orientdb by orientechnologies.

the class OQueryOperatorTraverse method traverse.

@SuppressWarnings("unchecked")
private boolean traverse(Object iTarget, final OSQLFilterCondition iCondition, final int iLevel, final Set<ORID> iEvaluatedRecords, final OCommandContext iContext) {
    if (endDeepLevel > -1 && iLevel > endDeepLevel)
        return false;
    if (iTarget instanceof OIdentifiable) {
        if (iEvaluatedRecords.contains(((OIdentifiable) iTarget).getIdentity()))
            // ALREADY EVALUATED
            return false;
        // TRANSFORM THE ORID IN ODOCUMENT
        iTarget = ((OIdentifiable) iTarget).getRecord();
    }
    if (iTarget instanceof ODocument) {
        final ODocument target = (ODocument) iTarget;
        iEvaluatedRecords.add(target.getIdentity());
        if (target.getInternalStatus() == ORecordElement.STATUS.NOT_LOADED)
            try {
                target.load();
            } catch (final ORecordNotFoundException e) {
                // INVALID RID
                return false;
            }
        if (iLevel >= startDeepLevel && (Boolean) iCondition.evaluate(target, null, iContext) == Boolean.TRUE)
            return true;
        // TRAVERSE THE DOCUMENT ITSELF
        if (cfgFields != null)
            for (final String cfgField : cfgFields) {
                if (cfgField.equalsIgnoreCase(OSQLFilterItemFieldAny.FULL_NAME)) {
                    // ANY
                    for (final String fieldName : target.fieldNames()) if (traverse(target.rawField(fieldName), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return true;
                } else if (cfgField.equalsIgnoreCase(OSQLFilterItemFieldAny.FULL_NAME)) {
                    // ALL
                    for (final String fieldName : target.fieldNames()) if (!traverse(target.rawField(fieldName), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return false;
                    return true;
                } else {
                    if (traverse(target.rawField(cfgField), iCondition, iLevel + 1, iEvaluatedRecords, iContext))
                        return true;
                }
            }
    } else if (iTarget instanceof OQueryRuntimeValueMulti) {
        final OQueryRuntimeValueMulti multi = (OQueryRuntimeValueMulti) iTarget;
        for (final Object o : multi.getValues()) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (iTarget instanceof Map<?, ?>) {
        final Map<Object, Object> map = (Map<Object, Object>) iTarget;
        for (final Object o : map.values()) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (OMultiValue.isMultiValue(iTarget)) {
        final Iterable<Object> collection = OMultiValue.getMultiValueIterable(iTarget, false);
        for (final Object o : collection) {
            if (traverse(o, iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    } else if (iTarget instanceof Iterator) {
        final Iterator iterator = (Iterator) iTarget;
        while (iterator.hasNext()) {
            if (traverse(iterator.next(), iCondition, iLevel + 1, iEvaluatedRecords, iContext) == Boolean.TRUE)
                return true;
        }
    }
    return false;
}
Also used : Iterator(java.util.Iterator) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Map(java.util.Map) OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with OQueryRuntimeValueMulti

use of com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti in project orientdb by orientechnologies.

the class OSQLFilterCondition method checkForConversion.

private Object[] checkForConversion(final OIdentifiable o, Object l, Object r, final OCollate collate) {
    Object[] result = null;
    final Object oldL = l;
    final Object oldR = r;
    if (collate != null) {
        l = collate.transform(l);
        r = collate.transform(r);
        if (l != oldL || r != oldR) // CHANGED
        {
            result = new Object[] { l, r };
        }
    }
    try {
        // DEFINED OPERATOR
        if ((oldR instanceof String && oldR.equals(OSQLHelper.DEFINED)) || (oldL instanceof String && oldL.equals(OSQLHelper.DEFINED))) {
            result = new Object[] { ((OSQLFilterItemAbstract) this.left).getRoot(), r };
        } else // NOT_NULL OPERATOR
        if ((oldR instanceof String && oldR.equals(OSQLHelper.NOT_NULL)) || (oldL instanceof String && oldL.equals(OSQLHelper.NOT_NULL))) {
            result = null;
        } else if (l != null && r != null && !l.getClass().isAssignableFrom(r.getClass()) && !r.getClass().isAssignableFrom(l.getClass())) // INTEGERS
        {
            if (r instanceof Integer && !(l instanceof Number || l instanceof Collection)) {
                if (l instanceof String && ((String) l).indexOf('.') > -1) {
                    result = new Object[] { new Float((String) l).intValue(), r };
                } else if (l instanceof Date) {
                    result = new Object[] { ((Date) l).getTime(), r };
                } else if (!(l instanceof OQueryRuntimeValueMulti) && !(l instanceof Collection<?>) && !l.getClass().isArray() && !(l instanceof Map)) {
                    result = new Object[] { getInteger(l), r };
                }
            } else if (l instanceof Integer && !(r instanceof Number || r instanceof Collection)) {
                if (r instanceof String && ((String) r).indexOf('.') > -1) {
                    result = new Object[] { l, new Float((String) r).intValue() };
                } else if (r instanceof Date) {
                    result = new Object[] { l, ((Date) r).getTime() };
                } else if (!(r instanceof OQueryRuntimeValueMulti) && !(r instanceof Collection<?>) && !r.getClass().isArray() && !(r instanceof Map)) {
                    result = new Object[] { l, getInteger(r) };
                }
            } else // DATES
            if (r instanceof Date && !(l instanceof Collection || l instanceof Date)) {
                result = new Object[] { getDate(l), r };
            } else if (l instanceof Date && !(r instanceof Collection || r instanceof Date)) {
                result = new Object[] { l, getDate(r) };
            } else // FLOATS
            if (r instanceof Float && !(l instanceof Float || l instanceof Collection)) {
                result = new Object[] { getFloat(l), r };
            } else if (l instanceof Float && !(r instanceof Float || r instanceof Collection)) {
                result = new Object[] { l, getFloat(r) };
            } else // RIDS
            if (r instanceof ORID && l instanceof String && !oldL.equals(OSQLHelper.NOT_NULL)) {
                result = new Object[] { new ORecordId((String) l), r };
            } else if (l instanceof ORID && r instanceof String && !oldR.equals(OSQLHelper.NOT_NULL)) {
                result = new Object[] { l, new ORecordId((String) r) };
            }
        }
    } catch (Exception e) {
    // JUST IGNORE CONVERSION ERRORS
    }
    return result;
}
Also used : OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ParseException(java.text.ParseException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OQueryParsingException(com.orientechnologies.orient.core.exception.OQueryParsingException) ORID(com.orientechnologies.orient.core.id.ORID)

Example 4 with OQueryRuntimeValueMulti

use of com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti in project orientdb by orientechnologies.

the class OSQLFilterItemFieldMultiAbstract method getValue.

public Object getValue(final OIdentifiable iRecord, Object iCurrentResult, OCommandContext iContext) {
    final ODocument doc = ((ODocument) iRecord);
    if (names.size() == 1)
        return transformValue(iRecord, iContext, ODocumentHelper.getIdentifiableValue(iRecord, names.get(0)));
    final String[] fieldNames = doc.fieldNames();
    final Object[] values = new Object[fieldNames.length];
    collates.clear();
    for (int i = 0; i < values.length; ++i) {
        values[i] = doc.field(fieldNames[i]);
        collates.add(getCollateForField(clazz, fieldNames[i]));
    }
    if (hasChainOperators()) {
        // TRANSFORM ALL THE VALUES
        for (int i = 0; i < values.length; ++i) values[i] = transformValue(iRecord, iContext, values[i]);
    }
    return new OQueryRuntimeValueMulti(this, values, collates);
}
Also used : OQueryRuntimeValueMulti(com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OQueryRuntimeValueMulti (com.orientechnologies.orient.core.query.OQueryRuntimeValueMulti)4 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 OException (com.orientechnologies.common.exception.OException)1 OCollate (com.orientechnologies.orient.core.collate.OCollate)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OQueryParsingException (com.orientechnologies.orient.core.exception.OQueryParsingException)1 ORID (com.orientechnologies.orient.core.id.ORID)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OBinaryField (com.orientechnologies.orient.core.serialization.serializer.record.binary.OBinaryField)1 ParseException (java.text.ParseException)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1