use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateCluster method execute.
/**
* Execute the CREATE CLUSTER.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (clusterName == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
final ODatabaseDocument database = getDatabase();
final int clusterId = database.getClusterIdByName(clusterName);
if (clusterId > -1)
throw new OCommandSQLParsingException("Cluster '" + clusterName + "' already exists");
if (blob) {
if (requestedId == -1) {
return database.addBlobCluster(clusterName);
} else {
throw new OCommandExecutionException("Request id not supported by blob cluster creation.");
}
} else {
if (requestedId == -1) {
return database.addCluster(clusterName);
} else {
return database.addCluster(clusterName, requestedId, null);
}
}
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateFunction method parse.
@SuppressWarnings("unchecked")
public OCommandExecutorSQLCreateFunction parse(final OCommandRequest iRequest) {
final OCommandRequestText textRequest = (OCommandRequestText) iRequest;
String queryText = textRequest.getText();
String originalQuery = queryText;
try {
queryText = preParse(queryText, iRequest);
textRequest.setText(queryText);
init((OCommandRequestText) iRequest);
parserRequiredKeyword("CREATE");
parserRequiredKeyword("FUNCTION");
name = parserNextWord(false);
code = OIOUtils.getStringContent(parserNextWord(false));
String temp = parseOptionalWord(true);
while (temp != null) {
if (temp.equals("IDEMPOTENT")) {
parserNextWord(false);
idempotent = Boolean.parseBoolean(parserGetLastWord());
} else if (temp.equals("LANGUAGE")) {
parserNextWord(false);
language = parserGetLastWord();
} else if (temp.equals("PARAMETERS")) {
parserNextWord(false);
parameters = new ArrayList<String>();
OStringSerializerHelper.getCollection(parserGetLastWord(), 0, parameters);
if (parameters.size() == 0)
throw new OCommandExecutionException("Syntax Error. Missing function parameter(s): " + getSyntax());
}
temp = parserOptionalWord(true);
if (parserIsEnded())
break;
}
} finally {
textRequest.setText(originalQuery);
}
return this;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateFunction method execute.
/**
* Execute the command and return the ODocument object created.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (name == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
if (name.isEmpty())
throw new OCommandExecutionException("Syntax Error. You must specify a function name: " + getSyntax());
if (code == null || code.isEmpty())
throw new OCommandExecutionException("Syntax Error. You must specify the function code: " + getSyntax());
ODatabaseDocument database = getDatabase();
final OFunction f = database.getMetadata().getFunctionLibrary().createFunction(name);
f.setCode(code);
f.setIdempotent(idempotent);
if (parameters != null)
f.setParameters(parameters);
if (language != null)
f.setLanguage(language);
f.save();
return f.getId();
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLCreateLink method execute.
/**
* Execute the CREATE LINK.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (destField == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
final ODatabaseDocumentInternal database = getDatabase();
if (!(database.getDatabaseOwner() instanceof ODatabaseDocument))
throw new OCommandSQLParsingException("This command supports only the database type ODatabaseDocumentTx and type '" + database.getClass() + "' was found");
final ODatabaseDocument db = (ODatabaseDocument) database.getDatabaseOwner();
final OClass sourceClass = database.getMetadata().getSchema().getClass(sourceClassName);
if (sourceClass == null)
throw new OCommandExecutionException("Source class '" + sourceClassName + "' not found");
final OClass destClass = database.getMetadata().getSchema().getClass(destClassName);
if (destClass == null)
throw new OCommandExecutionException("Destination class '" + destClassName + "' not found");
Object value;
String cmd = "select from ";
if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField)) {
cmd = "select from " + destClassName + " where " + destField + " = ";
}
List<ODocument> result;
ODocument target;
Object oldValue;
long total = 0;
if (linkName == null)
// NO LINK NAME EXPRESSED: OVERWRITE THE SOURCE FIELD
linkName = sourceField;
boolean multipleRelationship;
if (linkType != null)
// DETERMINE BASED ON FORCED TYPE
multipleRelationship = linkType == OType.LINKSET || linkType == OType.LINKLIST;
else
multipleRelationship = false;
long totRecords = db.countClass(sourceClass.getName());
long currRecord = 0;
if (progressListener != null)
progressListener.onBegin(this, totRecords, false);
database.declareIntent(new OIntentMassiveInsert());
try {
// BROWSE ALL THE RECORDS OF THE SOURCE CLASS
for (ODocument doc : db.browseClass(sourceClass.getName())) {
value = doc.field(sourceField);
if (value != null) {
if (value instanceof ODocument || value instanceof ORID) {
// ALREADY CONVERTED
} else if (value instanceof Collection<?>) {
// TODO
} else {
// SEARCH THE DESTINATION RECORD
target = null;
if (!ODocumentHelper.ATTRIBUTE_RID.equals(destField) && value instanceof String)
if (((String) value).length() == 0)
value = null;
else
value = "'" + value + "'";
result = database.<OCommandRequest>command(new OSQLSynchQuery<ODocument>(cmd + value)).execute();
if (result == null || result.size() == 0)
value = null;
else if (result.size() > 1)
throw new OCommandExecutionException("Cannot create link because multiple records was found in class '" + destClass.getName() + "' with value " + value + " in field '" + destField + "'");
else {
target = result.get(0);
value = target;
}
if (target != null && inverse) {
// INVERSE RELATIONSHIP
oldValue = target.field(linkName);
if (oldValue != null) {
if (!multipleRelationship)
multipleRelationship = true;
Collection<ODocument> coll;
if (oldValue instanceof Collection) {
// ADD IT IN THE EXISTENT COLLECTION
coll = (Collection<ODocument>) oldValue;
target.setDirty();
} else {
// CREATE A NEW COLLECTION FOR BOTH
coll = new ArrayList<ODocument>(2);
target.field(linkName, coll);
coll.add((ODocument) oldValue);
}
coll.add(doc);
} else {
if (linkType != null)
if (linkType == OType.LINKSET) {
value = new ORecordLazySet(target);
((Set<OIdentifiable>) value).add(doc);
} else if (linkType == OType.LINKLIST) {
value = new ORecordLazyList(target);
((ORecordLazyList) value).add(doc);
} else
// IGNORE THE TYPE, SET IT AS LINK
value = doc;
else
value = doc;
target.field(linkName, value);
}
target.save();
} else {
// SET THE REFERENCE
doc.field(linkName, value);
doc.save();
}
total++;
}
}
if (progressListener != null)
progressListener.onProgress(this, currRecord, currRecord * 100f / totRecords);
}
if (total > 0) {
if (inverse) {
// REMOVE THE OLD PROPERTY IF ANY
OProperty prop = destClass.getProperty(linkName);
if (prop != null)
destClass.dropProperty(linkName);
if (linkType == null)
linkType = multipleRelationship ? OType.LINKSET : OType.LINK;
// CREATE THE PROPERTY
destClass.createProperty(linkName, linkType, sourceClass);
} else {
// REMOVE THE OLD PROPERTY IF ANY
OProperty prop = sourceClass.getProperty(linkName);
if (prop != null)
sourceClass.dropProperty(linkName);
// CREATE THE PROPERTY
sourceClass.createProperty(linkName, OType.LINK, destClass);
}
}
if (progressListener != null)
progressListener.onCompletition(this, true);
} catch (Exception e) {
if (progressListener != null)
progressListener.onCompletition(this, false);
throw OException.wrapException(new OCommandExecutionException("Error on creation of links"), e);
} finally {
database.declareIntent(null);
}
return total;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException 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();
}
Aggregations