use of com.orientechnologies.orient.core.sql.method.misc.OSQLMethodField 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().getClusterId() < 0) {
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