use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable 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;
}
}
use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable 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.OSQLFilterItemVariable in project orientdb by orientechnologies.
the class OSQLFunctionIntersect method execute.
public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams, OCommandContext iContext) {
Object value = iParams[0];
if (value instanceof OSQLFilterItemVariable)
value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);
if (value == null)
return Collections.emptySet();
if (iParams.length == 1) {
// AGGREGATION MODE (STATEFUL)
if (context == null) {
// ADD ALL THE ITEMS OF THE FIRST COLLECTION
if (value instanceof Collection) {
context = ((Collection) value).iterator();
} else if (value instanceof Iterator) {
context = (Iterator) value;
} else {
context = Arrays.asList(value).iterator();
}
} else {
context = intersectWith((Iterator) context, value);
}
return null;
}
// IN-LINE MODE (STATELESS)
Iterator iterator = OMultiValue.getMultiValueIterator(value, false);
for (int i = 1; i < iParams.length; ++i) {
value = iParams[i];
if (value instanceof OSQLFilterItemVariable)
value = ((OSQLFilterItemVariable) value).getValue(iCurrentRecord, iCurrentResult, iContext);
if (value != null) {
value = intersectWith(iterator, value);
iterator = OMultiValue.getMultiValueIterator(value, false);
} else {
return new ArrayList().iterator();
}
}
return iterator;
}
use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItemVariable in project orientdb by orientechnologies.
the class ORuntimeResult method applyRecord.
@SuppressWarnings("unchecked")
public static ODocument applyRecord(final ODocument iValue, final Map<String, Object> iProjections, final OCommandContext iContext, final OIdentifiable iRecord) {
// APPLY PROJECTIONS
ORecord record = (iRecord != null ? iRecord.getRecord() : null);
//MANAGE SPECIFIC CASES FOR RECORD BYTES
if (ORecordBytes.RECORD_TYPE == ORecordInternal.getRecordType(record)) {
for (Entry<String, Object> projection : iProjections.entrySet()) {
if ("@rid".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getIdentity());
} else if ("@size".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getSize());
} else if ("@version".equalsIgnoreCase("" + projection.getValue())) {
iValue.field(projection.getKey(), record.getVersion());
} else {
Object val = projection.getValue();
if (val instanceof Number || val instanceof String || val instanceof Boolean) {
iValue.field(projection.getKey(), val);
} else {
iValue.field(projection.getKey(), (Object) null);
}
}
}
return iValue;
}
final ODocument inputDocument = (ODocument) record;
if (iProjections.isEmpty())
// SELECT * CASE
inputDocument.copyTo(iValue);
else {
for (Entry<String, Object> projection : iProjections.entrySet()) {
final String prjName = projection.getKey();
final Object v = projection.getValue();
if (v == null && prjName != null) {
iValue.field(prjName, (Object) null);
continue;
}
final Object projectionValue;
if (v != null && v.equals("*")) {
// COPY ALL
inputDocument.copyTo(iValue);
// CONTINUE WITH NEXT ITEM
continue;
} else if (v instanceof OSQLFilterItemVariable || v instanceof OSQLFilterItemField) {
final OSQLFilterItemAbstract var = (OSQLFilterItemAbstract) v;
final OPair<OSQLMethodRuntime, Object[]> last = var.getLastChainOperator();
if (last != null && last.getKey().getMethod() instanceof OSQLMethodField && last.getValue() != null && last.getValue().length == 1 && last.getValue()[0].equals("*")) {
final Object value = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
if (inputDocument != null && value != null && inputDocument instanceof ODocument && value instanceof ODocument) {
// COPY FIELDS WITH PROJECTION NAME AS PREFIX
for (String fieldName : ((ODocument) value).fieldNames()) {
iValue.field(prjName + fieldName, ((ODocument) value).field(fieldName));
}
}
projectionValue = null;
} else
// RETURN A VARIABLE FROM THE CONTEXT
projectionValue = ((OSQLFilterItemAbstract) v).getValue(inputDocument, iValue, iContext);
} else if (v instanceof OSQLFunctionRuntime) {
final OSQLFunctionRuntime f = (OSQLFunctionRuntime) v;
projectionValue = f.execute(inputDocument, inputDocument, iValue, iContext);
} else {
if (v == null) {
// SIMPLE NULL VALUE: SET IT IN DOCUMENT
iValue.field(prjName, v);
continue;
}
projectionValue = v;
}
if (projectionValue != null)
if (projectionValue instanceof ORidBag)
iValue.field(prjName, new ORidBag((ORidBag) projectionValue));
else if (projectionValue instanceof OIdentifiable && !(projectionValue instanceof ORID) && !(projectionValue instanceof ORecord))
iValue.field(prjName, ((OIdentifiable) projectionValue).getRecord());
else if (projectionValue instanceof Iterator) {
boolean link = true;
// make temporary value typical case graph database elemenet's iterator edges
if (projectionValue instanceof OResettable)
((OResettable) projectionValue).reset();
final List<Object> iteratorValues = new ArrayList<Object>();
final Iterator projectionValueIterator = (Iterator) projectionValue;
while (projectionValueIterator.hasNext()) {
Object value = projectionValueIterator.next();
if (value instanceof OIdentifiable) {
value = ((OIdentifiable) value).getRecord();
if (value != null && !((OIdentifiable) value).getIdentity().isPersistent())
link = false;
}
if (value != null)
iteratorValues.add(value);
}
iValue.field(prjName, iteratorValues, link ? OType.LINKLIST : OType.EMBEDDEDLIST);
} else if (projectionValue instanceof ODocument && !((ODocument) projectionValue).getIdentity().isPersistent()) {
iValue.field(prjName, projectionValue, OType.EMBEDDED);
} else if (projectionValue instanceof Set<?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKSET && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
type = OType.EMBEDDEDSET;
iValue.field(prjName, projectionValue, type);
} else if (projectionValue instanceof Map<?, ?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKMAP && !entriesPersistent(((Map<?, OIdentifiable>) projectionValue).values()))
type = OType.EMBEDDEDMAP;
iValue.field(prjName, projectionValue, type);
} else if (projectionValue instanceof List<?>) {
OType type = OType.getTypeByValue(projectionValue);
if (type == OType.LINKLIST && !entriesPersistent((Collection<OIdentifiable>) projectionValue))
type = OType.EMBEDDEDLIST;
iValue.field(prjName, projectionValue, type);
} else
iValue.field(prjName, projectionValue);
}
}
return iValue;
}
Aggregations