Search in sources :

Example 1 with OClassImpl

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

the class OCommandExecutorSQLDropProperty method execute.

/**
   * Execute the CREATE PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (fieldName == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not yet been parsed");
    final ODatabaseDocument database = getDatabase();
    final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    if (ifExists && !sourceClass.existsProperty(fieldName)) {
        return null;
    }
    final List<OIndex<?>> indexes = relatedIndexes(fieldName);
    if (!indexes.isEmpty()) {
        if (force) {
            dropRelatedIndexes(indexes);
        } else {
            final StringBuilder indexNames = new StringBuilder();
            boolean first = true;
            for (final OIndex<?> index : sourceClass.getClassInvolvedIndexes(fieldName)) {
                if (!first) {
                    indexNames.append(", ");
                } else {
                    first = false;
                }
                indexNames.append(index.getName());
            }
            throw new OCommandExecutionException("Property used in indexes (" + indexNames.toString() + "). Please drop these indexes before removing property or use FORCE parameter.");
        }
    }
    // REMOVE THE PROPERTY
    sourceClass.dropProperty(fieldName);
    return null;
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl)

Example 2 with OClassImpl

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

the class OIndexManagerShared method notifyInvolvedClasses.

protected void notifyInvolvedClasses(int[] clusterIdsToIndex) {
    if (clusterIdsToIndex == null || clusterIdsToIndex.length == 0)
        return;
    final ODatabaseDocumentInternal database = getDatabase();
    // UPDATE INVOLVED CLASSES
    final Set<String> classes = new HashSet<String>();
    for (int clusterId : clusterIdsToIndex) {
        final OClass cls = database.getMetadata().getSchema().getClassByClusterId(clusterId);
        if (cls != null && cls instanceof OClassImpl && !classes.contains(cls.getName())) {
            ((OClassImpl) cls).onPostIndexManagement();
            classes.add(cls.getName());
        }
    }
}
Also used : OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 3 with OClassImpl

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

the class OSequenceLibraryImpl method init.

private void init() {
    final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
    if (db.getMetadata().getSchema().existsClass(OSequence.CLASS_NAME)) {
        return;
    }
    final OClassImpl sequenceClass = (OClassImpl) db.getMetadata().getSchema().createClass(OSequence.CLASS_NAME);
    OSequence.initClass(sequenceClass);
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl)

Example 4 with OClassImpl

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

the class OCommandExecutorSQLCreateProperty method execute.

/**
   * Execute the CREATE PROPERTY.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (type == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final ODatabaseDocument database = getDatabase();
    final OClassImpl sourceClass = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (sourceClass == null)
        throw new OCommandExecutionException("Source class '" + className + "' not found");
    OPropertyImpl prop = (OPropertyImpl) sourceClass.getProperty(fieldName);
    if (prop != null) {
        if (ifNotExists) {
            return sourceClass.properties().size();
        }
        throw new OCommandExecutionException("Property '" + className + "." + fieldName + "' already exists. Remove it before to retry.");
    }
    // CREATE THE PROPERTY
    OClass linkedClass = null;
    OType linkedType = null;
    if (linked != null) {
        // FIRST SEARCH BETWEEN CLASSES
        linkedClass = database.getMetadata().getSchema().getClass(linked);
        if (linkedClass == null)
            // NOT FOUND: SEARCH BETWEEN TYPES
            linkedType = OType.valueOf(linked.toUpperCase(Locale.ENGLISH));
    }
    // CREATE IT LOCALLY
    OPropertyImpl internalProp = sourceClass.addPropertyInternal(fieldName, type, linkedType, linkedClass, unsafe);
    boolean toSave = false;
    if (readonly) {
        internalProp.setReadonly(true);
        toSave = true;
    }
    if (mandatory) {
        internalProp.setMandatory(true);
        toSave = true;
    }
    if (notnull) {
        internalProp.setNotNull(true);
        toSave = true;
    }
    if (max != null) {
        internalProp.setMax(max);
        toSave = true;
    }
    if (min != null) {
        internalProp.setMin(min);
        toSave = true;
    }
    if (defaultValue != null) {
        internalProp.setDefaultValue(defaultValue);
        toSave = true;
    }
    if (collate != null) {
        internalProp.setCollate(collate);
        toSave = true;
    }
    if (regex != null) {
        internalProp.setRegexp(regex);
        toSave = true;
    }
    if (toSave) {
        internalProp.save();
    }
    return sourceClass.properties().size();
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OType(com.orientechnologies.orient.core.metadata.schema.OType) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)

Example 5 with OClassImpl

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

the class OCommandExecutorSQLAlterClass method execute.

/**
   * Execute the ALTER CLASS.
   */
public Object execute(final Map<Object, Object> iArgs) {
    final ODatabaseDocument database = getDatabase();
    if (attribute == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    final OClassImpl cls = (OClassImpl) database.getMetadata().getSchema().getClass(className);
    if (cls == null)
        throw new OCommandExecutionException("Cannot alter class '" + className + "' because not found");
    if (!unsafe && attribute == ATTRIBUTES.NAME && cls.isSubClassOf("E"))
        throw new OCommandExecutionException("Cannot alter class '" + className + "' because is an Edge class and could break vertices. Use UNSAFE if you want to force it");
    // REMOVE CACHE OF COMMAND RESULTS
    for (int clId : cls.getPolymorphicClusterIds()) getDatabase().getMetadata().getCommandCache().invalidateResultsOfCluster(getDatabase().getClusterNameById(clId));
    if (value != null && attribute == ATTRIBUTES.SUPERCLASS) {
        checkClassExists(database, className, decodeClassName(value));
    }
    if (value != null && attribute == ATTRIBUTES.SUPERCLASSES) {
        List<String> classes = Arrays.asList(value.split(",\\s*"));
        for (String cName : classes) {
            checkClassExists(database, className, decodeClassName(cName));
        }
    }
    if (!unsafe && value != null && attribute == ATTRIBUTES.NAME) {
        if (!cls.getIndexes().isEmpty()) {
            throw new OCommandExecutionException("Cannot rename class '" + className + "' because it has indexes defined on it. Drop indexes before or use UNSAFE (at your won risk)");
        }
    }
    cls.set(attribute, value);
    return Boolean.TRUE;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl)

Aggregations

OClassImpl (com.orientechnologies.orient.core.metadata.schema.OClassImpl)8 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)5 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)4 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OPropertyImpl (com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)3 OIndex (com.orientechnologies.orient.core.index.OIndex)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)1 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)1 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ODistributedConfiguration (com.orientechnologies.orient.server.distributed.ODistributedConfiguration)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1