Search in sources :

Example 1 with IdentificationVariableNode

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

the class IdVarSelector method post.

@Override
public Object post(Object t) {
    if (!(t instanceof CommonTree))
        return t;
    if (t instanceof CommonErrorNode) {
        return t;
    }
    CommonTree node = (CommonTree) t;
    if (node.token == null)
        return t;
    if ((node instanceof QueryNode) && node.getParent() != null && "T_CONDITION".equals(((CommonTree) node.getParent()).token.getText())) {
        stack.peekLast().setPropagateVariablesUp(false);
        return t;
    }
    if (node instanceof IdentificationVariableNode) {
        IdentificationVariableNode vnode = (IdentificationVariableNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    if (node instanceof JoinVariableNode) {
        JoinVariableNode vnode = (JoinVariableNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    return t;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) CommonTree(org.antlr.runtime.tree.CommonTree) QueryNode(com.haulmont.cuba.core.sys.jpql.tree.QueryNode) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Example 2 with IdentificationVariableNode

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

the class QueryTransformerAstBased method getMainEntityName.

private String getMainEntityName() {
    if (mainEntityName == null) {
        IdentificationVariableNode mainEntityIdentification = getQueryTransformer().getMainEntityIdentification();
        if (mainEntityIdentification != null) {
            try {
                JpqlEntityModel entityByName = model.getEntityByName(mainEntityIdentification.getEntityNameFromQuery());
                mainEntityName = entityByName.getName();
            } catch (UnknownEntityNameException e) {
                throw new RuntimeException("Could not resolve entity for name " + mainEntityIdentification.getEntityNameFromQuery());
            }
        }
    }
    return mainEntityName;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 3 with IdentificationVariableNode

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

the class IdVarSelector method post.

@Override
public Object post(Object t) {
    if (!(t instanceof CommonTree))
        return t;
    if (t instanceof CommonErrorNode) {
        return t;
    }
    CommonTree node = (CommonTree) t;
    if (node.token == null)
        return t;
    if ((node instanceof QueryNode) && isInWhereSubquery(node)) {
        stack.peekLast().setPropagateVariablesUp(false);
        return t;
    }
    if (node instanceof IdentificationVariableNode) {
        IdentificationVariableNode vnode = (IdentificationVariableNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    if (node instanceof BaseJoinNode) {
        BaseJoinNode vnode = (BaseJoinNode) node;
        vnode.identifyVariableEntity(model, stack, invalidIdVarNodes);
        return t;
    }
    return t;
}
Also used : IdentificationVariableNode(com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode) BaseJoinNode(com.haulmont.cuba.core.sys.jpql.tree.BaseJoinNode) CommonTree(org.antlr.runtime.tree.CommonTree) QueryNode(com.haulmont.cuba.core.sys.jpql.tree.QueryNode) CommonErrorNode(org.antlr.runtime.tree.CommonErrorNode)

Example 4 with IdentificationVariableNode

use of com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode 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

IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)4 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)2 QueryNode (com.haulmont.cuba.core.sys.jpql.tree.QueryNode)2 CommonErrorNode (org.antlr.runtime.tree.CommonErrorNode)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)1 BaseJoinNode (com.haulmont.cuba.core.sys.jpql.tree.BaseJoinNode)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)1