use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OCommandExecutorSQLUpdate method handlePutEntries.
@SuppressWarnings({ "unchecked", "rawtypes" })
private boolean handlePutEntries(ODocument record) {
boolean updated = false;
if (!putEntries.isEmpty()) {
// BIND VALUES TO PUT (AS MAP)
for (OTriple<String, String, Object> entry : putEntries) {
Object fieldValue = record.field(entry.getKey());
if (fieldValue == null) {
if (ODocumentInternal.getImmutableSchemaClass(record) != null) {
final OProperty property = ODocumentInternal.getImmutableSchemaClass(record).getProperty(entry.getKey());
if (property != null && (property.getType() != null && (!property.getType().equals(OType.EMBEDDEDMAP) && !property.getType().equals(OType.LINKMAP)))) {
throw new OCommandExecutionException("field " + entry.getKey() + " is not defined as a map");
}
}
fieldValue = new HashMap<String, Object>();
record.field(entry.getKey(), fieldValue);
}
if (fieldValue instanceof Map<?, ?>) {
Map<String, Object> map = (Map<String, Object>) fieldValue;
OPair<String, Object> pair = entry.getValue();
Object value = extractValue(record, pair);
if (record.getSchemaClass() != null) {
final OProperty property = record.getSchemaClass().getProperty(entry.getKey());
if (property != null && property.getType().equals(OType.LINKMAP) && !(value instanceof OIdentifiable)) {
throw new OCommandExecutionException("field " + entry.getKey() + " defined of type LINKMAP accept only link values");
}
}
if (OType.LINKMAP.equals(OType.getTypeByValue(fieldValue)) && !(value instanceof OIdentifiable)) {
map = new OTrackedMap(record, map, Object.class);
record.field(entry.getKey(), map, OType.EMBEDDEDMAP);
}
map.put(pair.getKey(), value);
updated = true;
}
}
}
return updated;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OSQLTarget method extractTargets.
@SuppressWarnings("unchecked")
private boolean extractTargets() {
parserSkipWhiteSpaces();
if (parserIsEnded())
throw new OQueryParsingException("No query target found", parserText, 0);
final char c = parserGetCurrentChar();
if (c == '$') {
targetVariable = parserRequiredWord(false, "No valid target");
targetVariable = targetVariable.substring(1);
} else if (c == OStringSerializerHelper.LINK || Character.isDigit(c)) {
// UNIQUE RID
targetRecords = new ArrayList<OIdentifiable>();
((List<OIdentifiable>) targetRecords).add(new ORecordId(parserRequiredWord(true, "No valid RID")));
} else if (c == OStringSerializerHelper.EMBEDDED_BEGIN) {
// SUB QUERY
final StringBuilder subText = new StringBuilder(256);
parserSetCurrentPosition(OStringSerializerHelper.getEmbedded(parserText, parserGetCurrentPosition(), -1, subText) + 1);
final OCommandSQL subCommand = new OCommandSQLResultset(subText.toString());
final OCommandExecutorSQLResultsetDelegate executor = (OCommandExecutorSQLResultsetDelegate) OCommandManager.instance().getExecutor(subCommand);
executor.setProgressListener(subCommand.getProgressListener());
executor.parse(subCommand);
OCommandContext childContext = executor.getContext();
if (childContext != null) {
childContext.setParent(context);
}
if (!(executor instanceof Iterable<?>))
throw new OCommandSQLParsingException("Sub-query cannot be iterated because doesn't implement the Iterable interface: " + subCommand);
targetQuery = subText.toString();
targetRecords = executor;
} else if (c == OStringSerializerHelper.LIST_BEGIN) {
// COLLECTION OF RIDS
final List<String> rids = new ArrayList<String>();
parserSetCurrentPosition(OStringSerializerHelper.getCollection(parserText, parserGetCurrentPosition(), rids));
targetRecords = new ArrayList<OIdentifiable>();
for (String rid : rids) ((List<OIdentifiable>) targetRecords).add(new ORecordId(rid));
parserMoveCurrentPosition(1);
} else {
while (!parserIsEnded() && (targetClasses == null && targetClusters == null && targetIndex == null && targetIndexValues == null && targetRecords == null)) {
String originalSubjectName = parserRequiredWord(false, "Target not found");
String subjectName = originalSubjectName.toUpperCase();
final String alias;
if (subjectName.equals("AS"))
alias = parserRequiredWord(true, "Alias not found");
else
alias = subjectName;
final String subjectToMatch = subjectName;
if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.CLUSTER_PREFIX)) {
// REGISTER AS CLUSTER
if (targetClusters == null)
targetClusters = new HashMap<String, String>();
final String clusterNames = subjectName.substring(OCommandExecutorSQLAbstract.CLUSTER_PREFIX.length());
if (clusterNames.startsWith("[") && clusterNames.endsWith("]")) {
final Collection<String> clusters = new HashSet<String>(3);
OStringSerializerHelper.getCollection(clusterNames, 0, clusters);
for (String cl : clusters) {
targetClusters.put(cl, cl);
}
} else
targetClusters.put(clusterNames, alias);
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_PREFIX)) {
// REGISTER AS INDEX
targetIndex = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_PREFIX.length());
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.METADATA_PREFIX)) {
// METADATA
final String metadataTarget = subjectName.substring(OCommandExecutorSQLAbstract.METADATA_PREFIX.length());
targetRecords = new ArrayList<OIdentifiable>();
if (metadataTarget.equals(OCommandExecutorSQLAbstract.METADATA_SCHEMA)) {
((ArrayList<OIdentifiable>) targetRecords).add(new ORecordId(ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().schemaRecordId));
} else if (metadataTarget.equals(OCommandExecutorSQLAbstract.METADATA_INDEXMGR)) {
((ArrayList<OIdentifiable>) targetRecords).add(new ORecordId(ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getConfiguration().indexMgrRecordId));
} else
throw new OQueryParsingException("Metadata element not supported: " + metadataTarget);
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.DICTIONARY_PREFIX)) {
// DICTIONARY
final String key = originalSubjectName.substring(OCommandExecutorSQLAbstract.DICTIONARY_PREFIX.length());
targetRecords = new ArrayList<OIdentifiable>();
final OIdentifiable value = ODatabaseRecordThreadLocal.INSTANCE.get().getDictionary().get(key);
if (value != null)
((List<OIdentifiable>) targetRecords).add(value);
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_PREFIX)) {
targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_PREFIX.length());
targetIndexValuesAsc = true;
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX)) {
targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_ASC_PREFIX.length());
targetIndexValuesAsc = true;
} else if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX)) {
targetIndexValues = subjectName.substring(OCommandExecutorSQLAbstract.INDEX_VALUES_DESC_PREFIX.length());
targetIndexValuesAsc = false;
} else {
if (subjectToMatch.startsWith(OCommandExecutorSQLAbstract.CLASS_PREFIX))
// REGISTER AS CLASS
subjectName = subjectName.substring(OCommandExecutorSQLAbstract.CLASS_PREFIX.length());
// REGISTER AS CLASS
if (targetClasses == null)
targetClasses = new HashMap<String, String>();
final OClass cls = ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata().getSchema().getClass(subjectName);
if (cls == null)
throw new OCommandExecutionException("Class '" + subjectName + "' was not found in database '" + ODatabaseRecordThreadLocal.INSTANCE.get().getName() + "'");
targetClasses.put(cls.getName(), alias);
}
}
}
return !parserIsEnded();
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OSQLFunctionTraversedElement method evaluate.
protected Object evaluate(final Object[] iParams, final OCommandContext iContext, final String iClassName) {
final int beginIndex = (Integer) iParams[0];
final int items = iParams.length > 1 ? (Integer) iParams[1] : 1;
final ArrayDeque stack = (ArrayDeque) iContext.getVariable("stack");
if (stack == null)
throw new OCommandExecutionException("Cannot invoke " + getName() + "() against non traverse command");
final List<OIdentifiable> result = items > 1 ? new ArrayList<OIdentifiable>(items) : null;
if (beginIndex < 0) {
int i = -1;
for (Iterator it = stack.iterator(); it.hasNext(); ) {
final Object o = it.next();
if (o instanceof OTraverseRecordProcess) {
final OIdentifiable record = ((OTraverseRecordProcess) o).getTarget();
if (iClassName == null || ODocumentInternal.getImmutableSchemaClass((ODocument) record.getRecord()).isSubClassOf(iClassName)) {
if (i <= beginIndex) {
if (items == 1)
return record;
else {
result.add(record);
if (result.size() >= items)
break;
}
}
i--;
}
}
}
} else {
int i = 0;
for (Iterator it = stack.descendingIterator(); it.hasNext(); ) {
final Object o = it.next();
if (o instanceof OTraverseRecordProcess) {
final OIdentifiable record = ((OTraverseRecordProcess) o).getTarget();
if (iClassName == null || ODocumentInternal.getImmutableSchemaClass((ODocument) record.getRecord()).isSubClassOf(iClassName)) {
if (i >= beginIndex) {
if (items == 1)
return record;
else {
result.add(record);
if (result.size() >= items)
break;
}
}
i++;
}
}
}
}
if (items > 0 && result != null && !result.isEmpty())
return result;
return null;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class OSQLFunctionSequence method execute.
@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, Object[] iParams, OCommandContext iContext) {
final String seqName;
if (configuredParameters[0] instanceof OSQLFilterItem)
seqName = (String) ((OSQLFilterItem) configuredParameters[0]).getValue(iCurrentRecord, iCurrentResult, iContext);
else
seqName = configuredParameters[0].toString();
OSequence result = ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata().getSequenceLibrary().getSequence(seqName);
if (result == null) {
throw new OCommandExecutionException("Sequence not found: " + seqName);
}
return result;
}
use of com.orientechnologies.orient.core.exception.OCommandExecutionException in project orientdb by orientechnologies.
the class ODefaultSQLMethodFactory method createMethod.
@Override
public OSQLMethod createMethod(final String name) throws OCommandExecutionException {
final Object m = methods.get(name);
final OSQLMethod method;
if (m instanceof Class<?>)
try {
method = (OSQLMethod) ((Class<?>) m).newInstance();
} catch (Exception e) {
throw OException.wrapException(new OCommandExecutionException("Cannot create SQL method: " + m), e);
}
else
method = (OSQLMethod) m;
if (method == null)
throw new OCommandExecutionException("Unknown method name: " + name);
return method;
}
Aggregations