use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.
the class OSQLFunctionRuntime method execute.
/**
* Execute a function.
*
* @param iCurrentRecord
* Current record
* @param iCurrentResult
* TODO
* @param iContext
* @return
*/
public Object execute(final Object iThis, final OIdentifiable iCurrentRecord, final Object iCurrentResult, final OCommandContext iContext) {
// RESOLVE VALUES USING THE CURRENT RECORD
for (int i = 0; i < configuredParameters.length; ++i) {
runtimeParameters[i] = configuredParameters[i];
if (configuredParameters[i] instanceof OSQLFilterItemField) {
runtimeParameters[i] = ((OSQLFilterItemField) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
} else if (configuredParameters[i] instanceof OSQLFunctionRuntime)
runtimeParameters[i] = ((OSQLFunctionRuntime) configuredParameters[i]).execute(iThis, iCurrentRecord, iCurrentResult, iContext);
else if (configuredParameters[i] instanceof OSQLFilterItemVariable) {
runtimeParameters[i] = ((OSQLFilterItemVariable) configuredParameters[i]).getValue(iCurrentRecord, iCurrentResult, iContext);
} else if (configuredParameters[i] instanceof OCommandSQL) {
try {
runtimeParameters[i] = ((OCommandSQL) configuredParameters[i]).setContext(iContext).execute();
} catch (OCommandExecutorNotFoundException e) {
// TRY WITH SIMPLE CONDITION
final String text = ((OCommandSQL) configuredParameters[i]).getText();
final OSQLPredicate pred = new OSQLPredicate(text);
runtimeParameters[i] = pred.evaluate(iCurrentRecord instanceof ORecord ? (ORecord) iCurrentRecord : null, (ODocument) iCurrentResult, iContext);
// REPLACE ORIGINAL PARAM
configuredParameters[i] = pred;
}
} else if (configuredParameters[i] instanceof OSQLPredicate)
runtimeParameters[i] = ((OSQLPredicate) configuredParameters[i]).evaluate(iCurrentRecord.getRecord(), (iCurrentRecord instanceof ODocument ? (ODocument) iCurrentResult : null), iContext);
else if (configuredParameters[i] instanceof String) {
if (configuredParameters[i].toString().startsWith("\"") || configuredParameters[i].toString().startsWith("'"))
runtimeParameters[i] = OIOUtils.getStringContent(configuredParameters[i]);
}
}
if (function.getMaxParams() == -1 || function.getMaxParams() > 0) {
if (runtimeParameters.length < function.getMinParams() || (function.getMaxParams() > -1 && runtimeParameters.length > function.getMaxParams()))
throw new OCommandExecutionException("Syntax error: function '" + function.getName() + "' needs " + (function.getMinParams() == function.getMaxParams() ? function.getMinParams() : function.getMinParams() + "-" + function.getMaxParams()) + " argument(s) while has been received " + runtimeParameters.length);
}
final Object functionResult = function.execute(iThis, iCurrentRecord, iCurrentResult, runtimeParameters, iContext);
if (functionResult instanceof OAutoConvertToRecord)
// FORCE AVOIDING TO CONVERT IN RECORD
((OAutoConvertToRecord) functionResult).setAutoConvertToRecord(false);
return transformValue(iCurrentRecord, iContext, functionResult);
}
use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField 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.OSQLFilterItemField in project orientdb by orientechnologies.
the class OQueryOperatorContainsText method filterRecords.
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public Collection<OIdentifiable> filterRecords(final ODatabase<?> iDatabase, final List<String> iTargetClasses, final OSQLFilterCondition iCondition, final Object iLeft, final Object iRight) {
final String fieldName;
if (iCondition.getLeft() instanceof OSQLFilterItemField)
fieldName = iCondition.getLeft().toString();
else
fieldName = iCondition.getRight().toString();
final String fieldValue;
if (iCondition.getLeft() instanceof OSQLFilterItemField)
fieldValue = iCondition.getRight().toString();
else
fieldValue = iCondition.getLeft().toString();
final String className = iTargetClasses.get(0);
final OProperty prop = ((OMetadataInternal) iDatabase.getMetadata()).getImmutableSchemaSnapshot().getClass(className).getProperty(fieldName);
if (prop == null)
// NO PROPERTY DEFINED
return null;
OIndex<?> fullTextIndex = null;
for (final OIndex<?> indexDefinition : prop.getIndexes()) {
if (indexDefinition instanceof OIndexFullText) {
fullTextIndex = indexDefinition;
break;
}
}
if (fullTextIndex == null) {
return null;
}
return (Collection<OIdentifiable>) fullTextIndex.get(fieldValue);
}
use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.
the class OFilterAnalyzer method createIndexedProperty.
/**
* Add SQL filter field to the search candidate list.
*
* @param iCondition Condition item
* @param iItem Value to search
* @return true if the property was indexed and found, otherwise false
*/
private OIndexSearchResult createIndexedProperty(final OSQLFilterCondition iCondition, final Object iItem, OCommandContext ctx) {
if (iItem == null || !(iItem instanceof OSQLFilterItemField)) {
return null;
}
if (iCondition.getLeft() instanceof OSQLFilterItemField && iCondition.getRight() instanceof OSQLFilterItemField) {
return null;
}
final OSQLFilterItemField item = (OSQLFilterItemField) iItem;
if (item.hasChainOperators() && !item.isFieldChain()) {
return null;
}
final Object origValue = iCondition.getLeft() == iItem ? iCondition.getRight() : iCondition.getLeft();
OQueryOperator operator = iCondition.getOperator();
if (iCondition.getRight() == iItem) {
if (operator instanceof OQueryOperatorIn) {
operator = new OQueryOperatorContains();
} else if (operator instanceof OQueryOperatorContains) {
operator = new OQueryOperatorIn();
}
}
if (iCondition.getOperator() instanceof OQueryOperatorBetween || operator instanceof OQueryOperatorIn) {
return new OIndexSearchResult(operator, item.getFieldChain(), origValue);
}
final Object value = OSQLHelper.getValue(origValue, null, ctx);
return new OIndexSearchResult(operator, item.getFieldChain(), value);
}
use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField in project orientdb by orientechnologies.
the class OLuceneOperatorUtil method buildOIndexSearchResult.
public static OIndexSearchResult buildOIndexSearchResult(OClass iSchemaClass, OSQLFilterCondition iCondition, List<OIndexSearchResult> iIndexSearchResults, OCommandContext context) {
if (iCondition.getLeft() instanceof Collection) {
OIndexSearchResult lastResult = null;
Collection left = (Collection) iCondition.getLeft();
int i = 0;
Object lastValue = null;
for (Object obj : left) {
if (obj instanceof OSQLFilterItemField) {
OSQLFilterItemField item = (OSQLFilterItemField) obj;
Object value = null;
if (iCondition.getRight() instanceof Collection) {
List<Object> right = (List<Object>) iCondition.getRight();
value = right.get(i);
} else {
value = iCondition.getRight();
}
if (lastResult == null) {
lastResult = new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value);
} else {
lastResult = lastResult.merge(new OIndexSearchResult(iCondition.getOperator(), item.getFieldChain(), value));
}
} else if (obj instanceof OSQLFilterItemVariable) {
OSQLFilterItemVariable item = (OSQLFilterItemVariable) obj;
Object value = null;
if (iCondition.getRight() instanceof Collection) {
List<Object> right = (List<Object>) iCondition.getRight();
value = right.get(i);
} else {
value = iCondition.getRight();
}
context.setVariable(item.toString(), value);
}
i++;
}
if (lastResult != null && OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, lastResult))
iIndexSearchResults.add(lastResult);
return lastResult;
} else {
OIndexSearchResult result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getLeft());
if (result == null)
result = OLuceneOperatorUtil.createIndexedProperty(iCondition, iCondition.getRight());
if (result == null)
return null;
if (OLuceneOperatorUtil.checkIndexExistence(iSchemaClass, result))
iIndexSearchResults.add(result);
return result;
}
}
Aggregations