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();
}
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations