Search in sources :

Example 1 with OSQLFilterItem

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItem in project orientdb by orientechnologies.

the class OTraverseRecordProcess method processFields.

private void processFields(Iterator<Object> target) {
    final ODocument doc = this.target.getRecord();
    while (target.hasNext()) {
        Object field = target.next();
        final Object fieldValue;
        if (field instanceof OSQLFilterItem)
            fieldValue = ((OSQLFilterItem) field).getValue(doc, null, null);
        else
            fieldValue = doc.rawField(field.toString());
        if (fieldValue != null) {
            final OTraverseAbstractProcess<?> subProcess;
            if (fieldValue instanceof Iterator<?> || OMultiValue.isMultiValue(fieldValue)) {
                final Iterator<?> coll;
                if (fieldValue instanceof ORecordLazyMultiValue)
                    coll = ((ORecordLazyMultiValue) fieldValue).rawIterator();
                else
                    coll = OMultiValue.getMultiValueIterator(fieldValue, false);
                subProcess = new OTraverseMultiValueProcess(command, (Iterator<Object>) coll, getPath().appendField(field.toString()));
            } else if (fieldValue instanceof OIdentifiable && ((OIdentifiable) fieldValue).getRecord() instanceof ODocument) {
                subProcess = new OTraverseRecordProcess(command, (ODocument) ((OIdentifiable) fieldValue).getRecord(), getPath().appendField(field.toString()));
            } else
                continue;
            command.getContext().push(subProcess);
        }
    }
}
Also used : Iterator(java.util.Iterator) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordLazyMultiValue(com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)

Example 2 with OSQLFilterItem

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItem in project orientdb by orientechnologies.

the class OCommandExecutorSQLCreateEdge method execute.

/**
   * Execute the command and return the ODocument object created.
   */
public Object execute(final Map<Object, Object> iArgs) {
    if (clazz == null)
        throw new OCommandExecutionException("Cannot execute the command because it has not been parsed yet");
    return OGraphCommandExecutorSQLFactory.runInConfiguredTxMode(new OGraphCommandExecutorSQLFactory.GraphCallBack<List<Object>>() {

        @Override
        public List<Object> call(OrientBaseGraph graph) {
            final Set<OIdentifiable> fromIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), from, context, iArgs);
            final Set<OIdentifiable> toIds = OSQLEngine.getInstance().parseRIDTarget(graph.getRawGraph(), to, context, iArgs);
            // CREATE EDGES
            final List<Object> edges = new ArrayList<Object>();
            for (OIdentifiable from : fromIds) {
                final OrientVertex fromVertex = graph.getVertex(from);
                if (fromVertex == null)
                    throw new OCommandExecutionException("Source vertex '" + from + "' not exists");
                for (OIdentifiable to : toIds) {
                    final OrientVertex toVertex;
                    if (from.equals(to)) {
                        toVertex = fromVertex;
                    } else {
                        toVertex = graph.getVertex(to);
                    }
                    if (fields != null)
                        // EVALUATE FIELDS
                        for (final OPair<String, Object> f : fields) {
                            if (f.getValue() instanceof OSQLFunctionRuntime) {
                                f.setValue(((OSQLFunctionRuntime) f.getValue()).getValue(to, null, context));
                            } else if (f.getValue() instanceof OSQLFilterItem) {
                                f.setValue(((OSQLFilterItem) f.getValue()).getValue(to, null, context));
                            }
                        }
                    OrientEdge edge = null;
                    if (content != null) {
                        if (fields != null)
                            // MERGE CONTENT WITH FIELDS
                            fields.addAll(OPair.convertFromMap(content.toMap()));
                        else
                            fields = OPair.convertFromMap(content.toMap());
                    }
                    edge = fromVertex.addEdge(null, toVertex, edgeLabel, clusterName, fields);
                    if (fields != null && !fields.isEmpty()) {
                        if (edge.isLightweight())
                            edge.convertToDocument();
                        OSQLHelper.bindParameters(edge.getRecord(), fields, new OCommandParameters(iArgs), context);
                    }
                    edge.save(clusterName);
                    edges.add(edge);
                    if (batch > 0 && edges.size() % batch == 0) {
                        graph.commit();
                        graph.begin();
                    }
                }
            }
            if (edges.isEmpty()) {
                if (fromIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no source vertices");
                else if (toIds.isEmpty())
                    throw new OCommandExecutionException("No edge has been created because no target vertices");
                throw new OCommandExecutionException("No edge has been created between " + fromIds + " and " + toIds);
            }
            return edges;
        }
    });
}
Also used : OSQLFunctionRuntime(com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException)

Example 3 with OSQLFilterItem

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItem 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;
}
Also used : OSequence(com.orientechnologies.orient.core.metadata.sequence.OSequence) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem)

Example 4 with OSQLFilterItem

use of com.orientechnologies.orient.core.sql.filter.OSQLFilterItem in project orientdb by orientechnologies.

the class OQueryOperatorIn method executeIndexQuery.

@SuppressWarnings("unchecked")
@Override
public OIndexCursor executeIndexQuery(OCommandContext iContext, OIndex<?> index, List<Object> keyParams, boolean ascSortOrder) {
    final OIndexDefinition indexDefinition = index.getDefinition();
    final OIndexInternal<?> internalIndex = index.getInternal();
    OIndexCursor cursor;
    if (!internalIndex.canBeUsedInEqualityOperators())
        return null;
    if (indexDefinition.getParamCount() == 1) {
        final Object inKeyValue = keyParams.get(0);
        Collection<Object> inParams;
        if (inKeyValue instanceof List<?>)
            inParams = (Collection<Object>) inKeyValue;
        else if (inKeyValue instanceof OSQLFilterItem)
            inParams = (Collection<Object>) ((OSQLFilterItem) inKeyValue).getValue(null, null, iContext);
        else
            inParams = Collections.singleton(inKeyValue);
        if (inParams instanceof OResultSet) {
            //manage IN (subquery)
            Set newInParams = new HashSet();
            for (Object o : ((OResultSet) inParams)) {
                if (o instanceof ODocument && ((ODocument) o).getIdentity().getClusterId() < -1) {
                    ODocument doc = (ODocument) o;
                    String[] fieldNames = doc.fieldNames();
                    if (fieldNames.length == 1) {
                        newInParams.add(doc.field(fieldNames[0]));
                    } else {
                        newInParams.add(o);
                    }
                } else {
                    newInParams.add(o);
                }
            }
            inParams = newInParams;
        }
        final List<Object> inKeys = new ArrayList<Object>();
        boolean containsNotCompatibleKey = false;
        for (final Object keyValue : inParams) {
            final Object key;
            if (indexDefinition instanceof OIndexDefinitionMultiValue)
                key = ((OIndexDefinitionMultiValue) indexDefinition).createSingleValue(OSQLHelper.getValue(keyValue));
            else
                key = indexDefinition.createValue(OSQLHelper.getValue(keyValue));
            if (key == null) {
                containsNotCompatibleKey = true;
                break;
            }
            inKeys.add(key);
        }
        if (containsNotCompatibleKey)
            return null;
        cursor = index.iterateEntries(inKeys, ascSortOrder);
    } else {
        final List<Object> partialKey = new ArrayList<Object>();
        partialKey.addAll(keyParams);
        partialKey.remove(keyParams.size() - 1);
        final Object inKeyValue = keyParams.get(keyParams.size() - 1);
        final Collection<Object> inParams;
        if (inKeyValue instanceof List<?>)
            inParams = (Collection<Object>) inKeyValue;
        else if (inKeyValue instanceof OSQLFilterItem)
            inParams = (Collection<Object>) ((OSQLFilterItem) inKeyValue).getValue(null, null, iContext);
        else
            throw new IllegalArgumentException("Key '" + inKeyValue + "' is not valid");
        final List<Object> inKeys = new ArrayList<Object>();
        final OCompositeIndexDefinition compositeIndexDefinition = (OCompositeIndexDefinition) indexDefinition;
        boolean containsNotCompatibleKey = false;
        for (final Object keyValue : inParams) {
            List<Object> fullKey = new ArrayList<Object>();
            fullKey.addAll(partialKey);
            fullKey.add(keyValue);
            final Object key = compositeIndexDefinition.createSingleValue(fullKey);
            if (key == null) {
                containsNotCompatibleKey = true;
                break;
            }
            inKeys.add(key);
        }
        if (containsNotCompatibleKey) {
            return null;
        }
        if (inKeys == null)
            return null;
        if (indexDefinition.getParamCount() == keyParams.size()) {
            final Object indexResult;
            indexResult = index.iterateEntries(inKeys, ascSortOrder);
            if (indexResult == null || indexResult instanceof OIdentifiable) {
                cursor = new OIndexCursorSingleValue((OIdentifiable) indexResult, inKeys);
            } else if (indexResult instanceof OIndexCursor) {
                cursor = (OIndexCursor) indexResult;
            } else {
                cursor = new OIndexCursorCollectionValue((Collection<OIdentifiable>) indexResult, inKeys);
            }
        } else
            return null;
    }
    updateProfiler(iContext, internalIndex, keyParams, indexDefinition);
    return cursor;
}
Also used : OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) OSQLFilterItem(com.orientechnologies.orient.core.sql.filter.OSQLFilterItem) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OResultSet(com.orientechnologies.orient.core.sql.query.OResultSet) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OSQLFilterItem (com.orientechnologies.orient.core.sql.filter.OSQLFilterItem)4 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)3 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)2 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)1 OSequence (com.orientechnologies.orient.core.metadata.sequence.OSequence)1 OSQLFunctionRuntime (com.orientechnologies.orient.core.sql.functions.OSQLFunctionRuntime)1 OResultSet (com.orientechnologies.orient.core.sql.query.OResultSet)1 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)1 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)1 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)1 Iterator (java.util.Iterator)1