use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OSQLFilterCondition method evaluate.
public Object evaluate(final OIdentifiable iCurrentRecord, final ODocument iCurrentResult, final OCommandContext iContext) {
boolean binaryEvaluation = operator != null && operator.isSupportingBinaryEvaluate() && iCurrentRecord != null && iCurrentRecord.getIdentity().isPersistent();
if (left instanceof OSQLQuery<?>)
// EXECUTE SUB QUERIES ONLY ONCE
left = ((OSQLQuery<?>) left).setContext(iContext).execute();
Object l = evaluate(iCurrentRecord, iCurrentResult, left, iContext, binaryEvaluation);
if (operator == null || operator.canShortCircuit(l))
return l;
if (right instanceof OSQLQuery<?>)
// EXECUTE SUB QUERIES ONLY ONCE
right = ((OSQLQuery<?>) right).setContext(iContext).execute();
Object r = evaluate(iCurrentRecord, iCurrentResult, right, iContext, binaryEvaluation);
if (binaryEvaluation && l instanceof OBinaryField) {
if (r != null && !(r instanceof OBinaryField)) {
final OType type = OType.getTypeByValue(r);
if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
final BytesContainer bytes = new BytesContainer();
ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, r, type, null);
bytes.offset = 0;
final OCollate collate = r instanceof OSQLFilterItemField ? ((OSQLFilterItemField) r).getCollate(iCurrentRecord) : null;
r = new OBinaryField(null, type, bytes, collate);
if (!(right instanceof OSQLFilterItem || right instanceof OSQLFilterCondition))
// FIXED VALUE, REPLACE IT
right = r;
}
} else if (r instanceof OBinaryField)
// GET THE COPY OR MT REASONS
r = ((OBinaryField) r).copy();
}
if (binaryEvaluation && r instanceof OBinaryField) {
if (l != null && !(l instanceof OBinaryField)) {
final OType type = OType.getTypeByValue(l);
if (ORecordSerializerBinary.INSTANCE.getCurrentSerializer().getComparator().isBinaryComparable(type)) {
final BytesContainer bytes = new BytesContainer();
ORecordSerializerBinary.INSTANCE.getCurrentSerializer().serializeValue(bytes, l, type, null);
bytes.offset = 0;
final OCollate collate = l instanceof OSQLFilterItemField ? ((OSQLFilterItemField) l).getCollate(iCurrentRecord) : null;
l = new OBinaryField(null, type, bytes, collate);
if (!(left instanceof OSQLFilterItem || left instanceof OSQLFilterCondition))
// FIXED VALUE, REPLACE IT
left = l;
}
} else if (l instanceof OBinaryField)
// GET THE COPY OR MT REASONS
l = ((OBinaryField) l).copy();
}
if (binaryEvaluation)
binaryEvaluation = l instanceof OBinaryField && r instanceof OBinaryField;
if (!binaryEvaluation) {
// no collate for regular expressions, otherwise quotes will result in no match
final OCollate collate = operator instanceof OQueryOperatorMatches ? null : getCollate(iCurrentRecord);
final Object[] convertedValues = checkForConversion(iCurrentRecord, l, r, collate);
if (convertedValues != null) {
l = convertedValues[0];
r = convertedValues[1];
}
}
Object result;
try {
result = operator.evaluateRecord(iCurrentRecord, iCurrentResult, this, l, r, iContext);
} catch (OCommandExecutionException e) {
throw e;
} catch (Exception e) {
if (OLogManager.instance().isDebugEnabled())
OLogManager.instance().debug(this, "Error on evaluating expression (%s)", e, toString());
result = Boolean.FALSE;
}
return result;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLTruncateClass method execute.
/**
* Execute the command.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (schemaClass == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
final long recs = schemaClass.count(deep);
if (recs > 0 && !unsafe) {
if (schemaClass.isSubClassOf("V")) {
throw new OCommandExecutionException("'TRUNCATE CLASS' command cannot be used on not empty vertex classes. Apply the 'UNSAFE' keyword to force it (at your own risk)");
} else if (schemaClass.isSubClassOf("E")) {
throw new OCommandExecutionException("'TRUNCATE CLASS' command cannot be used on not empty edge classes. Apply the 'UNSAFE' keyword to force it (at your own risk)");
}
}
Collection<OClass> subclasses = schemaClass.getAllSubclasses();
if (deep && !unsafe) {
// for multiple inheritance
for (OClass subclass : subclasses) {
long subclassRecs = schemaClass.count();
if (subclassRecs > 0) {
if (subclass.isSubClassOf("V")) {
throw new OCommandExecutionException("'TRUNCATE CLASS' command cannot be used on not empty vertex classes (" + subclass.getName() + "). Apply the 'UNSAFE' keyword to force it (at your own risk)");
} else if (subclass.isSubClassOf("E")) {
throw new OCommandExecutionException("'TRUNCATE CLASS' command cannot be used on not empty edge classes (" + subclass.getName() + "). Apply the 'UNSAFE' keyword to force it (at your own risk)");
}
}
}
}
try {
schemaClass.truncate();
invalidateCommandCache(schemaClass);
if (deep) {
for (OClass subclass : subclasses) {
subclass.truncate();
invalidateCommandCache(subclass);
}
}
} catch (IOException e) {
throw OException.wrapException(new OCommandExecutionException("Error on executing command"), e);
}
return recs;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLTruncateRecord method execute.
/**
* Execute the command.
*/
public Object execute(final Map<Object, Object> iArgs) {
if (records.isEmpty())
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
int deleted = 0;
final ODatabaseDocumentInternal database = getDatabase();
for (String rec : records) {
try {
final ORecordId rid = new ORecordId(rec);
final OStorageOperationResult<Boolean> result = database.getStorage().deleteRecord(rid, -1, 0, null);
database.getLocalCache().deleteRecord(rid);
if (result.getResult())
deleted++;
} catch (Throwable e) {
throw OException.wrapException(new OCommandExecutionException("Error on executing command"), e);
}
}
return deleted;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLUpdate method execute.
public Object execute(final Map<Object, Object> iArgs) {
if (subjectName == null)
throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
parameters = new OCommandParameters(iArgs);
Map<Object, Object> queryArgs;
if (parameters.size() > 0 && parameters.getByName(0) != null) {
queryArgs = new HashMap<Object, Object>();
for (int i = parameterCounter; i < parameters.size(); i++) {
if (parameters.getByName(i) != null)
queryArgs.put(i - parameterCounter, parameters.getByName(i));
}
} else {
queryArgs = iArgs;
}
query.setContext(context);
returnHandler.reset();
if (lockStrategy.equals("RECORD"))
query.getContext().setVariable("$locking", OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK);
getDatabase().query(query, queryArgs);
if (upsertMode && !updated) {
// IF UPDATE DOES NOT PRODUCE RESULTS AND UPSERT MODE IS ENABLED, CREATE DOCUMENT AND APPLY SET/ADD/PUT/MERGE and so on
final ODocument doc = subjectName != null ? new ODocument(subjectName) : new ODocument();
final String suspendedLockStrategy = lockStrategy;
// New record hasn't been created under exclusive lock - just to avoid releasing locks by result(doc)
lockStrategy = "NONE";
try {
result(doc);
} catch (ORecordDuplicatedException e) {
if (upsertMode)
// UPDATE THE NEW RECORD
getDatabase().query(query, queryArgs);
else
throw e;
} catch (ORecordNotFoundException e) {
if (upsertMode)
// UPDATE THE NEW RECORD
getDatabase().query(query, queryArgs);
else
throw e;
} catch (OConcurrentModificationException e) {
if (upsertMode)
// UPDATE THE NEW RECORD
getDatabase().query(query, queryArgs);
else
throw e;
}
lockStrategy = suspendedLockStrategy;
}
return returnHandler.ret();
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException 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;
}
Aggregations