Search in sources :

Example 16 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OClassImpl method addPropertyInternal.

public OPropertyImpl addPropertyInternal(final String name, final OType type, final OType linkedType, final OClass linkedClass, final boolean unsafe) {
    if (name == null || name.length() == 0)
        throw new OSchemaException("Found property name null");
    final Character wrongCharacter = OSchemaShared.checkFieldNameIfValid(name);
    if (wrongCharacter != null)
        throw new OSchemaException("Invalid property name '" + name + "'. Character '" + wrongCharacter + "' cannot be used");
    if (!unsafe)
        checkPersistentPropertyType(getDatabase(), name, type);
    final String lowerName = name.toLowerCase();
    final OPropertyImpl prop;
    // This check are doubled becouse used by sql commands
    if (linkedType != null)
        OPropertyImpl.checkLinkTypeSupport(type);
    if (linkedClass != null)
        OPropertyImpl.checkSupportLinkedClass(type);
    acquireSchemaWriteLock();
    try {
        checkEmbedded();
        if (properties.containsKey(lowerName))
            throw new OSchemaException("Class '" + this.name + "' already has property '" + name + "'");
        OGlobalProperty global = owner.findOrCreateGlobalProperty(name, type);
        prop = new OPropertyImpl(this, global);
        properties.put(lowerName, prop);
        if (linkedType != null)
            prop.setLinkedTypeInternal(linkedType);
        else if (linkedClass != null)
            prop.setLinkedClassInternal(linkedClass);
    } finally {
        releaseSchemaWriteLock();
    }
    if (prop != null && !unsafe)
        fireDatabaseMigration(getDatabase(), name, type);
    return prop;
}
Also used : OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 17 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OClassImpl method addProperty.

private OProperty addProperty(final String propertyName, final OType type, final OType linkedType, final OClass linkedClass, final boolean unsafe) {
    if (type == null)
        throw new OSchemaException("Property type not defined.");
    if (propertyName == null || propertyName.length() == 0)
        throw new OSchemaException("Property name is null or empty");
    if (getDatabase().getStorage().getConfiguration().isStrictSql()) {
        validatePropertyName(propertyName);
    }
    if (getDatabase().getTransaction().isActive())
        throw new OSchemaException("Cannot create property '" + propertyName + "' inside a transaction");
    final ODatabaseDocumentInternal database = getDatabase();
    database.checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_UPDATE);
    if (linkedType != null)
        OPropertyImpl.checkLinkTypeSupport(type);
    if (linkedClass != null)
        OPropertyImpl.checkSupportLinkedClass(type);
    acquireSchemaWriteLock();
    try {
        final StringBuilder cmd = new StringBuilder("create property ");
        // CLASS.PROPERTY NAME
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        cmd.append(name);
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        cmd.append('.');
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        cmd.append(propertyName);
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        // TYPE
        cmd.append(' ');
        cmd.append(type.name);
        if (linkedType != null) {
            // TYPE
            cmd.append(' ');
            cmd.append(linkedType.name);
        } else if (linkedClass != null) {
            // TYPE
            cmd.append(' ');
            if (getDatabase().getStorage().getConfiguration().isStrictSql())
                cmd.append('`');
            cmd.append(linkedClass.getName());
            if (getDatabase().getStorage().getConfiguration().isStrictSql())
                cmd.append('`');
        }
        if (unsafe)
            cmd.append(" unsafe ");
        final OStorage storage = database.getStorage();
        if (storage instanceof OStorageProxy) {
            database.command(new OCommandSQL(cmd.toString())).execute();
            reload();
            return getProperty(propertyName);
        } else if (isDistributedCommand()) {
            final OProperty prop = (OProperty) OScenarioThreadLocal.executeAsDistributed(new Callable<OProperty>() {

                @Override
                public OProperty call() throws Exception {
                    return addPropertyInternal(propertyName, type, linkedType, linkedClass, unsafe);
                }
            });
            final OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
            commandSQL.addExcludedNode(((OAutoshardedStorage) storage).getNodeId());
            database.command(commandSQL).execute();
            return prop;
        } else
            return (OProperty) OScenarioThreadLocal.executeAsDistributed(new Callable<OProperty>() {

                @Override
                public OProperty call() throws Exception {
                    return addPropertyInternal(propertyName, type, linkedType, linkedClass, unsafe);
                }
            });
    } finally {
        releaseSchemaWriteLock();
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Callable(java.util.concurrent.Callable)

Example 18 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OSchemaShared method doCreateClass.

private OClass doCreateClass(final String className, int[] clusterIds, int retry, OClass... superClasses) throws ClusterIdsAreEmptyException {
    OClass result;
    final ODatabaseDocumentInternal db = getDatabase();
    final OStorage storage = db.getStorage();
    StringBuilder cmd = null;
    getDatabase().checkSecurity(ORule.ResourceGeneric.SCHEMA, ORole.PERMISSION_CREATE);
    if (superClasses != null)
        OClassImpl.checkParametersConflict(Arrays.asList(superClasses));
    acquireSchemaWriteLock();
    try {
        final String key = className.toLowerCase();
        if (classes.containsKey(key) && retry == 0)
            throw new OSchemaException("Class '" + className + "' already exists in current database");
        if (!executeThroughDistributedStorage())
            checkClustersAreAbsent(clusterIds);
        if (clusterIds == null || clusterIds.length == 0) {
            clusterIds = createClusters(className, getDatabase().getStorage().getConfiguration().getMinimumClusters());
        }
        cmd = new StringBuilder("create class ");
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        cmd.append(className);
        if (getDatabase().getStorage().getConfiguration().isStrictSql())
            cmd.append('`');
        List<OClass> superClassesList = new ArrayList<OClass>();
        if (superClasses != null && superClasses.length > 0) {
            boolean first = true;
            for (OClass superClass : superClasses) {
                // Filtering for null
                if (superClass != null) {
                    if (first)
                        cmd.append(" extends ");
                    else
                        cmd.append(", ");
                    cmd.append('`').append(superClass.getName()).append('`');
                    first = false;
                    superClassesList.add(superClass);
                }
            }
        }
        if (clusterIds != null) {
            if (clusterIds.length == 1 && clusterIds[0] == -1)
                cmd.append(" abstract");
            else {
                cmd.append(" cluster ");
                for (int i = 0; i < clusterIds.length; ++i) {
                    if (i > 0)
                        cmd.append(',');
                    else
                        cmd.append(' ');
                    cmd.append(clusterIds[i]);
                }
            }
        }
        if (executeThroughDistributedStorage()) {
            createClassInternal(className, clusterIds, superClassesList);
            final OAutoshardedStorage autoshardedStorage = (OAutoshardedStorage) storage;
            OCommandSQL commandSQL = new OCommandSQL(cmd.toString());
            commandSQL.addExcludedNode(autoshardedStorage.getNodeId());
            final Object res = db.command(commandSQL).execute();
        } else if (storage instanceof OStorageProxy) {
            db.command(new OCommandSQL(cmd.toString())).execute();
            reload();
        } else
            createClassInternal(className, clusterIds, superClassesList);
        result = classes.get(className.toLowerCase());
        // WAKE UP DB LIFECYCLE LISTENER
        for (Iterator<ODatabaseLifecycleListener> it = Orient.instance().getDbLifecycleListeners(); it.hasNext(); ) it.next().onCreateClass(getDatabase(), result);
    } finally {
        releaseSchemaWriteLock();
    }
    return result;
}
Also used : OStorageProxy(com.orientechnologies.orient.core.storage.OStorageProxy) OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAutoshardedStorage(com.orientechnologies.orient.core.storage.OAutoshardedStorage) ODatabaseLifecycleListener(com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)

Example 19 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OSchemaShared method createGlobalProperty.

public OGlobalProperty createGlobalProperty(final String name, final OType type, final Integer id) {
    OGlobalProperty global;
    if (id < properties.size() && (global = properties.get(id)) != null) {
        if (!global.getName().equals(name) || !global.getType().equals(type))
            throw new OSchemaException("A property with id " + id + " already exist ");
        return global;
    }
    global = new OGlobalPropertyImpl(name, type, id);
    ensurePropertiesSize(id);
    properties.set(id, global);
    propertiesByNameType.put(global.getName() + "|" + global.getType().name(), global);
    return global;
}
Also used : OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Example 20 with OSchemaException

use of com.orientechnologies.orient.core.exception.OSchemaException in project orientdb by orientechnologies.

the class OSchemaShared method addClusterForClass.

void addClusterForClass(final int clusterId, final OClass cls) {
    acquireSchemaWriteLock();
    try {
        if (!clustersCanNotBeSharedAmongClasses)
            return;
        if (clusterId < 0)
            return;
        final OStorage storage = getDatabase().getStorage();
        checkEmbedded(storage);
        final OClass existingCls = clustersToClasses.get(clusterId);
        if (existingCls != null && !cls.equals(existingCls))
            throw new OSchemaException("Cluster with id " + clusterId + " already belongs to class " + clustersToClasses.get(clusterId));
        clustersToClasses.put(clusterId, cls);
    } finally {
        releaseSchemaWriteLock();
    }
}
Also used : OStorage(com.orientechnologies.orient.core.storage.OStorage) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException)

Aggregations

OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)26 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)10 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)10 OException (com.orientechnologies.common.exception.OException)7 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)5 OStorage (com.orientechnologies.orient.core.storage.OStorage)5 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)4 ODatabaseLifecycleListener (com.orientechnologies.orient.core.db.ODatabaseLifecycleListener)3 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)3 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)3 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)3 OObjectNotDetachedException (com.orientechnologies.orient.object.db.OObjectNotDetachedException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)2 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)2