Search in sources :

Example 6 with PathNode

use of com.haulmont.cuba.core.sys.jpql.tree.PathNode in project cuba by cuba-platform.

the class VariableManipulator method pre.

@Override
public Object pre(Object o) {
    // todo unsafe, if we change a tree and it declares its own variables
    if (o instanceof PathNode) {
        PathNode pathNode = (PathNode) o;
        variableUses.add(pathNode);
    }
    return o;
}
Also used : PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode)

Example 7 with PathNode

use of com.haulmont.cuba.core.sys.jpql.tree.PathNode in project cuba by cuba-platform.

the class QueryParserAstBased method getQueryPaths.

@Override
public List<QueryPath> getQueryPaths() {
    List<QueryPath> queryPaths = new ArrayList<>();
    QueryVariableContext context = getQueryAnalyzer().getRootQueryVariableContext();
    TreeVisitor visitor = new TreeVisitor();
    PathNodeFinder finder = new PathNodeFinder();
    visitor.visit(getQueryAnalyzer().getTree(), finder);
    for (PathNode node : finder.getSelectedPathNodes()) {
        JpqlEntityModel model = context.getEntityByVariableNameHierarchically(node.getEntityVariableName());
        QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), true);
        queryPaths.add(queryPath);
    }
    for (PathNode node : finder.getOtherPathNodes()) {
        JpqlEntityModel model = context.getEntityByVariableNameHierarchically(node.getEntityVariableName());
        QueryPath queryPath = new QueryPath(model.getName(), node.getEntityVariableName(), node.asPathString(), false);
        queryPaths.add(queryPath);
    }
    return queryPaths;
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) ArrayList(java.util.ArrayList) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 8 with PathNode

use of com.haulmont.cuba.core.sys.jpql.tree.PathNode in project cuba by cuba-platform.

the class QueryParserAstBased method getEntityNameAndPathIfSecondaryReturnedInsteadOfMain.

protected EntityNameAndPath getEntityNameAndPathIfSecondaryReturnedInsteadOfMain() {
    List<PathNode> returnedPathNodes = getQueryAnalyzer().getReturnedPathNodes();
    if (CollectionUtils.isEmpty(returnedPathNodes) || returnedPathNodes.size() > 1) {
        return null;
    }
    QueryVariableContext rootQueryVariableContext = getQueryAnalyzer().getRootQueryVariableContext();
    PathNode pathNode = returnedPathNodes.get(0);
    if (pathNode.getChildren() == null) {
        JpqlEntityModel entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
        if (entity != null) {
            if (!Objects.equals(entity.getName(), getEntityName())) {
                return new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName());
            }
            // fix for scary Eclipselink which consider "select p from sec$GroupHierarchy h join h.parent p"
            // (even if h.parent is also sec$GroupHierarchy)
            // as report query and does not allow to set view
            IdentificationVariableNode mainEntityIdentification = getQueryAnalyzer().getMainEntityIdentification();
            if (mainEntityIdentification != null && !pathNode.getEntityVariableName().equals(mainEntityIdentification.getVariableName())) {
                return entity.getName() != null ? new EntityNameAndPath(entity.getName(), pathNode.getEntityVariableName()) : null;
            }
        }
        return null;
    }
    JpqlEntityModel entity;
    String entityPath;
    boolean collectionSelect = false;
    try {
        entity = rootQueryVariableContext.getEntityByVariableName(pathNode.getEntityVariableName());
        if (entity != null) {
            entityPath = pathNode.asPathString();
            for (int i = 0; i < pathNode.getChildCount(); i++) {
                String fieldName = pathNode.getChild(i).toString();
                Attribute entityAttribute = entity.getAttributeByName(fieldName);
                if (entityAttribute != null && entityAttribute.isEntityReferenceAttribute()) {
                    entity = model.getEntityByName(entityAttribute.getReferencedEntityName());
                    if (!collectionSelect) {
                        collectionSelect = entityAttribute.isCollection();
                    }
                } else {
                    return null;
                }
            }
        } else {
            return null;
        }
    } catch (UnknownEntityNameException e) {
        throw new RuntimeException(format("Unable to find entity by name %s", e.getEntityName()), e);
    }
    return entity != null && entity.getName() != null ? new EntityNameAndPath(entity.getName(), entityPath, collectionSelect) : null;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) Attribute(com.haulmont.cuba.core.sys.jpql.model.Attribute) PathNode(com.haulmont.cuba.core.sys.jpql.tree.PathNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Aggregations

PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)8 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)4 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)2 ArrayList (java.util.ArrayList)2 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)1 VirtualJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel)1 HasEntityPointer (com.haulmont.cuba.core.sys.jpql.pointer.HasEntityPointer)1 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 TreeVisitor (org.antlr.runtime.tree.TreeVisitor)1 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)1