Search in sources :

Example 1 with OSQLFilterCondition

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

the class OCommandExecutorSQLDelete method execute.

public Object execute(final Map<Object, Object> iArgs) {
    if (query == null && indexName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    if (!returning.equalsIgnoreCase("COUNT"))
        allDeletedRecords = new ArrayList<ORecord>();
    if (query != null) {
        // AGAINST CLUSTERS AND CLASSES
        query.setContext(getContext());
        Object prevLockValue = query.getContext().getVariable("$locking");
        if (lockStrategy.equals("RECORD"))
            query.getContext().setVariable("$locking", OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK);
        query.execute(iArgs);
        query.getContext().setVariable("$locking", prevLockValue);
        if (returning.equalsIgnoreCase("COUNT"))
            // RETURNS ONLY THE COUNT
            return recordCount;
        else
            // RETURNS ALL THE DELETED RECORDS
            return allDeletedRecords;
    } else {
        // AGAINST INDEXES
        if (compiledFilter != null)
            compiledFilter.bindParameters(iArgs);
        final OIndex index = getDatabase().getMetadata().getIndexManager().getIndex(indexName);
        if (index == null)
            throw new OCommandExecutionException("Target index '" + indexName + "' not found");
        Object key = null;
        Object value = VALUE_NOT_FOUND;
        if (compiledFilter == null || compiledFilter.getRootCondition() == null) {
            if (returning.equalsIgnoreCase("COUNT")) {
                // RETURNS ONLY THE COUNT
                final long total = index.getSize();
                index.clear();
                return total;
            } else {
                // RETURNS ALL THE DELETED RECORDS
                OIndexCursor cursor = index.cursor();
                Map.Entry<Object, OIdentifiable> entry;
                while ((entry = cursor.nextEntry()) != null) {
                    OIdentifiable rec = entry.getValue();
                    rec = rec.getRecord();
                    if (rec != null)
                        allDeletedRecords.add((ORecord) rec);
                }
                index.clear();
                return allDeletedRecords;
            }
        } else {
            if (KEYWORD_KEY.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString()))
                // FOUND KEY ONLY
                key = getIndexKey(index.getDefinition(), compiledFilter.getRootCondition().getRight());
            else if (KEYWORD_RID.equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString())) {
                // BY RID
                value = OSQLHelper.getValue(compiledFilter.getRootCondition().getRight());
            } else if (compiledFilter.getRootCondition().getLeft() instanceof OSQLFilterCondition) {
                // KEY AND VALUE
                final OSQLFilterCondition leftCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getLeft();
                if (KEYWORD_KEY.equalsIgnoreCase(leftCondition.getLeft().toString()))
                    key = getIndexKey(index.getDefinition(), leftCondition.getRight());
                final OSQLFilterCondition rightCondition = (OSQLFilterCondition) compiledFilter.getRootCondition().getRight();
                if (KEYWORD_RID.equalsIgnoreCase(rightCondition.getLeft().toString()))
                    value = OSQLHelper.getValue(rightCondition.getRight());
            }
            final boolean result;
            if (value != VALUE_NOT_FOUND) {
                assert key != null;
                result = index.remove(key, (OIdentifiable) value);
            } else
                result = index.remove(key);
            if (returning.equalsIgnoreCase("COUNT"))
                return result ? 1 : 0;
            else
                // TODO: REFACTOR INDEX TO RETURN DELETED ITEMS
                throw new UnsupportedOperationException();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) ORecord(com.orientechnologies.orient.core.record.ORecord) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) Map(java.util.Map)

Example 2 with OSQLFilterCondition

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

the class OQueryOperatorNot method getEndRidRange.

@Override
public ORID getEndRidRange(Object iLeft, Object iRight) {
    if (iLeft instanceof OSQLFilterCondition) {
        final ORID beginRange = ((OSQLFilterCondition) iLeft).getBeginRidRange();
        final ORID endRange = ((OSQLFilterCondition) iLeft).getEndRidRange();
        if (beginRange == null && endRange == null)
            return null;
        else if (beginRange == null)
            return null;
        else if (endRange == null)
            return beginRange;
        else
            return null;
    }
    return null;
}
Also used : OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) ORID(com.orientechnologies.orient.core.id.ORID)

Example 3 with OSQLFilterCondition

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

the class OQueryOperatorOr method getBeginRidRange.

@Override
public ORID getBeginRidRange(final Object iLeft, final Object iRight) {
    final ORID leftRange;
    final ORID rightRange;
    if (iLeft instanceof OSQLFilterCondition)
        leftRange = ((OSQLFilterCondition) iLeft).getBeginRidRange();
    else
        leftRange = null;
    if (iRight instanceof OSQLFilterCondition)
        rightRange = ((OSQLFilterCondition) iRight).getBeginRidRange();
    else
        rightRange = null;
    if (leftRange == null || rightRange == null)
        return null;
    else
        return leftRange.compareTo(rightRange) <= 0 ? leftRange : rightRange;
}
Also used : OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) ORID(com.orientechnologies.orient.core.id.ORID)

Example 4 with OSQLFilterCondition

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

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

the class OQueryOperatorContainsAll 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.getClass().isArray()) {
        if (iRight.getClass().isArray()) {
            // ARRAY VS ARRAY
            int matches = 0;
            for (final Object l : (Object[]) iLeft) {
                for (final Object r : (Object[]) iRight) {
                    if (OQueryOperatorEquals.equals(l, r)) {
                        ++matches;
                        break;
                    }
                }
            }
            return matches == ((Object[]) iRight).length;
        } else if (iRight instanceof Collection<?>) {
            // ARRAY VS ARRAY
            int matches = 0;
            for (final Object l : (Object[]) iLeft) {
                for (final Object r : (Collection<?>) iRight) {
                    if (OQueryOperatorEquals.equals(l, r)) {
                        ++matches;
                        break;
                    }
                }
            }
            return matches == ((Collection<?>) iRight).size();
        }
    } else if (iLeft instanceof Collection<?>) {
        final Collection<ODocument> collection = (Collection<ODocument>) iLeft;
        if (condition != null) {
            // CHECK AGAINST A CONDITION
            for (final ODocument o : collection) {
                if ((Boolean) condition.evaluate(o, null, iContext) == Boolean.FALSE)
                    return false;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            for (final Object o : collection) {
                if (!OQueryOperatorEquals.equals(iRight, o))
                    return false;
            }
        }
    } else if (iRight instanceof Collection<?>) {
        // CHECK AGAINST A CONDITION
        final Collection<ODocument> collection = (Collection<ODocument>) iRight;
        if (condition != null) {
            for (final ODocument o : collection) {
                if ((Boolean) condition.evaluate(o, null, iContext) == Boolean.FALSE)
                    return false;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            for (final Object o : collection) {
                if (!OQueryOperatorEquals.equals(iLeft, o))
                    return false;
            }
        }
    }
    return true;
}
Also used : OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSQLFilterCondition (com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition)13 ORID (com.orientechnologies.orient.core.id.ORID)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OSQLFilterItemField (com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField)3 Map (java.util.Map)3 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OType (com.orientechnologies.orient.core.metadata.schema.OType)2 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORecord (com.orientechnologies.orient.core.record.ORecord)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OQueryOperator (com.orientechnologies.orient.core.sql.operator.OQueryOperator)1 OQueryOperatorEquals (com.orientechnologies.orient.core.sql.operator.OQueryOperatorEquals)1 OQueryOperatorNotEquals (com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals)1 OQueryOperatorNotEquals2 (com.orientechnologies.orient.core.sql.operator.OQueryOperatorNotEquals2)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1