Search in sources :

Example 16 with OImmutableClass

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;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) ArrayList(java.util.ArrayList) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecord(com.orientechnologies.orient.core.record.ORecord) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)16 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORecord (com.orientechnologies.orient.core.record.ORecord)6 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 Vertex (com.tinkerpop.blueprints.Vertex)2 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)2 OException (com.orientechnologies.common.exception.OException)1 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 ORID (com.orientechnologies.orient.core.id.ORID)1