Search in sources :

Example 56 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class OCommandExecutorSQLUpdate method handleAddEntries.

private boolean handleAddEntries(ODocument record) {
    boolean updated = false;
    // BIND VALUES TO ADD
    Object fieldValue;
    for (OPair<String, Object> entry : addEntries) {
        Collection<Object> coll = null;
        ORidBag bag = null;
        if (!record.containsField(entry.getKey())) {
            // GET THE TYPE IF ANY
            if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
                OProperty prop = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
                if (prop != null && prop.getType() == OType.LINKSET)
                    // SET TYPE
                    coll = new HashSet<Object>();
                if (prop != null && prop.getType() == OType.LINKBAG) {
                    // there is no ridbag value already but property type is defined as LINKBAG
                    bag = new ORidBag();
                    bag.setOwner(record);
                    record.field(entry.getKey(), bag);
                }
            }
            if (coll == null && bag == null)
                // IN ALL OTHER CASES USE A LIST
                coll = new ArrayList<Object>();
            if (coll != null) {
                // containField's condition above does NOT check subdocument's fields so
                Collection<Object> currColl = record.field(entry.getKey());
                if (currColl == null) {
                    record.field(entry.getKey(), coll);
                    coll = record.field(entry.getKey());
                } else
                    coll = currColl;
            }
        } else {
            fieldValue = record.field(entry.getKey());
            if (fieldValue instanceof Collection<?>)
                coll = (Collection<Object>) fieldValue;
            else if (fieldValue instanceof ORidBag)
                bag = (ORidBag) fieldValue;
            else
                continue;
        }
        final Object value = extractValue(record, entry);
        if (coll != null) {
            if (value instanceof OIdentifiable)
                coll.add(value);
            else
                OMultiValue.add(coll, value);
        } else {
            if (!(value instanceof OIdentifiable))
                throw new OCommandExecutionException("Only links or records can be added to LINKBAG");
            bag.add((OIdentifiable) value);
        }
        updated = true;
    }
    return updated;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 57 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class OCommandExecutorSQLUpdate method handlePutEntries.

@SuppressWarnings({ "unchecked", "rawtypes" })
private boolean handlePutEntries(ODocument record) {
    boolean updated = false;
    if (!putEntries.isEmpty()) {
        // BIND VALUES TO PUT (AS MAP)
        for (OTriple<String, String, Object> entry : putEntries) {
            Object fieldValue = record.field(entry.getKey());
            if (fieldValue == null) {
                if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
                    final OProperty property = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
                    if (property != null && (property.getType() != null && (!property.getType().equals(OType.EMBEDDEDMAP) && !property.getType().equals(OType.LINKMAP)))) {
                        throw new OCommandExecutionException("field " + entry.getKey() + " is not defined as a map");
                    }
                }
                fieldValue = new HashMap<String, Object>();
                record.field(entry.getKey(), fieldValue);
            }
            if (fieldValue instanceof Map<?, ?>) {
                Map<String, Object> map = (Map<String, Object>) fieldValue;
                OPair<String, Object> pair = entry.getValue();
                Object value = extractValue(record, pair);
                if (record.getSchemaClass() != null) {
                    final OProperty property = record.getSchemaClass().getProperty(entry.getKey());
                    if (property != null && property.getType().equals(OType.LINKMAP) && !(value instanceof OIdentifiable)) {
                        throw new OCommandExecutionException("field " + entry.getKey() + " defined of type LINKMAP accept only link values");
                    }
                }
                if (OType.LINKMAP.equals(OType.getTypeByValue(fieldValue)) && !(value instanceof OIdentifiable)) {
                    map = new OTrackedMap(record, map, Object.class);
                    record.field(entry.getKey(), map, OType.EMBEDDEDMAP);
                }
                map.put(pair.getKey(), value);
                updated = true;
            }
        }
    }
    return updated;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 58 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class OCommandExecutorSQLUpdate method handleContent.

private boolean handleContent(ODocument record) {
    boolean updated = false;
    if (content != null) {
        // REPLACE ALL THE CONTENT
        final ODocument fieldsToPreserve = new ODocument();
        final OClass restricted = getDatabase().getMetadata().getSchema().getClass(OSecurity.RESTRICTED_CLASSNAME);
        if (restricted != null && restricted.isSuperClassOf(record.getSchemaClass())) {
            for (OProperty prop : restricted.properties()) {
                fieldsToPreserve.field(prop.getName(), record.field(prop.getName()));
            }
        }
        OClass recordClass = ODocumentInternal.getImmutableSchemaClass(record);
        if (recordClass != null && recordClass.isSubClassOf("V")) {
            for (String fieldName : record.fieldNames()) {
                if (fieldName.startsWith("in_") || fieldName.startsWith("out_")) {
                    fieldsToPreserve.field(fieldName, record.field(fieldName));
                }
            }
        } else if (recordClass != null && recordClass.isSubClassOf("E")) {
            for (String fieldName : record.fieldNames()) {
                if (fieldName.equals("in") || fieldName.equals("out")) {
                    fieldsToPreserve.field(fieldName, record.field(fieldName));
                }
            }
        }
        record.merge(fieldsToPreserve, false, false);
        record.merge(content, true, false);
        updated = true;
    }
    return updated;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 59 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class OQueryOperatorContains 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;
    if (iLeft instanceof Iterable<?>) {
        final Iterable<Object> iterable = (Iterable<Object>) iLeft;
        if (condition != null) {
            // CHECK AGAINST A CONDITION
            for (final Object o : iterable) {
                final OIdentifiable id;
                if (o instanceof OIdentifiable)
                    id = (OIdentifiable) o;
                else if (o instanceof Map<?, ?>) {
                    final Iterator<Object> iter = ((Map<?, Object>) o).values().iterator();
                    final Object v = iter.hasNext() ? iter.next() : null;
                    if (v instanceof OIdentifiable)
                        id = (OIdentifiable) v;
                    else
                        // TRANSFORM THE ENTIRE MAP IN A DOCUMENT. PROBABLY HAS BEEN IMPORTED FROM JSON
                        id = new ODocument((Map) o);
                } else if (o instanceof Iterable<?>) {
                    final Iterator<OIdentifiable> iter = ((Iterable<OIdentifiable>) o).iterator();
                    id = iter.hasNext() ? iter.next() : null;
                } else
                    continue;
                if ((Boolean) condition.evaluate(id, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            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();
                        }
                    }
                }
            }
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iRight, o, type))
                    return true;
            }
        }
    } else if (iRight instanceof Iterable<?>) {
        // CHECK AGAINST A CONDITION
        final Iterable<OIdentifiable> iterable = (Iterable<OIdentifiable>) iRight;
        if (condition != null) {
            for (final OIdentifiable o : iterable) {
                if ((Boolean) condition.evaluate(o, null, iContext) == Boolean.TRUE)
                    return true;
            }
        } else {
            // CHECK AGAINST A SINGLE VALUE
            for (final Object o : iterable) {
                if (OQueryOperatorEquals.equals(iLeft, o))
                    return true;
            }
        }
    }
    return false;
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OType(com.orientechnologies.orient.core.metadata.schema.OType) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OSQLFilterCondition(com.orientechnologies.orient.core.sql.filter.OSQLFilterCondition) OSQLFilterItemField(com.orientechnologies.orient.core.sql.filter.OSQLFilterItemField) Iterator(java.util.Iterator) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 60 with OProperty

use of com.orientechnologies.orient.core.metadata.schema.OProperty in project orientdb by orientechnologies.

the class UniqueHashIndexForDate method testSimpleUniqueDateIndex.

@Test
public void testSimpleUniqueDateIndex() throws ParseException {
    ODatabaseDocument db = new ODatabaseDocumentTx("memory:" + UniqueHashIndexForDate.class.getSimpleName());
    db.create();
    try {
        OClass clazz = db.getMetadata().getSchema().createClass("test_edge");
        OProperty prop = clazz.createProperty("date", OType.DATETIME);
        prop.createIndex(INDEX_TYPE.UNIQUE);
        ODocument doc = new ODocument("test_edge");
        doc.field("date", "2015-03-24 08:54:49");
        ODocument doc1 = new ODocument("test_edge");
        doc1.field("date", "2015-03-24 08:54:49");
        db.save(doc);
        try {
            db.begin();
            db.save(doc1);
            doc1.field("date", "2015-03-24 08:54:49");
            db.save(doc1);
            db.commit();
            Assert.fail("expected exception for duplicate ");
        } catch (OException e) {
        }
    } finally {
        db.drop();
    }
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OException(com.orientechnologies.common.exception.OException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)87 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)69 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)32 Test (org.testng.annotations.Test)30 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)25 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)23 OType (com.orientechnologies.orient.core.metadata.schema.OType)14 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)9 OIndex (com.orientechnologies.orient.core.index.OIndex)7 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)6 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)6 Map (java.util.Map)6 Set (java.util.Set)6 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)5 Collection (java.util.Collection)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 Field (java.lang.reflect.Field)4 Test (org.junit.Test)4 OException (com.orientechnologies.common.exception.OException)3