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;
}
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();
}
}
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;
}
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;
}
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();
}
}
Aggregations