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