use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass in project orientdb by orientechnologies.
the class OSQLHelper method bindParameters.
public static ODocument bindParameters(final ODocument iDocument, final List<OPair<String, Object>> iFields, final OCommandParameters iArguments, final OCommandContext iContext) {
if (iFields == null)
return null;
// BIND VALUES
for (OPair<String, Object> field : iFields) {
final String fieldName = field.getKey();
Object fieldValue = field.getValue();
if (fieldValue != null) {
if (fieldValue instanceof OCommandSQL) {
final OCommandSQL cmd = (OCommandSQL) fieldValue;
cmd.getContext().setParent(iContext);
fieldValue = ODatabaseRecordThreadLocal.INSTANCE.get().command(cmd).execute();
// CHECK FOR CONVERSIONS
OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(iDocument);
if (immutableClass != null) {
final OProperty prop = immutableClass.getProperty(fieldName);
if (prop != null) {
if (prop.getType() == OType.LINK) {
if (OMultiValue.isMultiValue(fieldValue)) {
final int size = OMultiValue.getSize(fieldValue);
if (size == 1)
// GET THE FIRST ITEM AS UNIQUE LINK
fieldValue = OMultiValue.getFirstValue(fieldValue);
else if (size == 0)
// NO ITEMS, SET IT AS NULL
fieldValue = null;
}
}
} else if (immutableClass.isEdgeType() && ("out".equals(fieldName) || "in".equals(fieldName)) && (fieldValue instanceof List)) {
List lst = (List) fieldValue;
if (lst.size() == 1) {
fieldValue = lst.get(0);
}
}
}
if (OMultiValue.isMultiValue(fieldValue)) {
final List<Object> tempColl = new ArrayList<Object>(OMultiValue.getSize(fieldValue));
String singleFieldName = null;
for (Object o : OMultiValue.getMultiValueIterable(fieldValue, false)) {
if (o instanceof OIdentifiable && !((OIdentifiable) o).getIdentity().isPersistent()) {
// TEMPORARY / EMBEDDED
final ORecord rec = ((OIdentifiable) o).getRecord();
if (rec != null && rec instanceof ODocument) {
// CHECK FOR ONE FIELD ONLY
final ODocument doc = (ODocument) rec;
if (doc.fields() == 1) {
singleFieldName = doc.fieldNames()[0];
tempColl.add(doc.field(singleFieldName));
} else {
// TRANSFORM IT IN EMBEDDED
doc.getIdentity().reset();
ODocumentInternal.addOwner(doc, iDocument);
ODocumentInternal.addOwner(doc, iDocument);
tempColl.add(doc);
}
}
} else
tempColl.add(o);
}
fieldValue = tempColl;
}
}
}
iDocument.field(fieldName, resolveFieldValue(iDocument, fieldName, fieldValue, iArguments, iContext));
}
return iDocument;
}
Aggregations