Search in sources :

Example 11 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryTransformerAstBased method addWhere.

private void addWhere(CommonTree whereTree, EntityReference ref, boolean replaceVariableName) {
    TreeVisitor visitor = new TreeVisitor();
    VariableManipulator variableManip = new VariableManipulator();
    visitor.visit(whereTree, variableManip);
    if (replaceVariableName) {
        Set<String> variables = variableManip.getUsedVariableNames();
        if (variables.size() > 1) {
            // we assume that adding where that use only one variable and does not add its own variables
            throw new IllegalStateException("Multiple variables used in condition");
        }
        String assumedEntityVariableInWhere = variableManip.getVariableNameInUse(0);
        variableManip.renameVariable(assumedEntityVariableInWhere, ref);
    }
    ParameterCounter parameterCounter = new ParameterCounter(true);
    visitor.visit(whereTree, parameterCounter);
    addedParams.addAll(parameterCounter.getParameterNames());
    getQueryTransformer().mixinWhereConditionsIntoTree(whereTree);
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor)

Example 12 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryTransformerAstBased method getResult.

@Override
public String getResult() {
    CommonTree tree = getQueryTransformer().getTree();
    TreeVisitor visitor = new TreeVisitor();
    TreeToQuery treeToQuery = new TreeToQuery();
    visitor.visit(tree, treeToQuery);
    return treeToQuery.getQueryString().trim();
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) CommonTree(org.antlr.runtime.tree.CommonTree)

Example 13 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class JoinVariableNode method toQuery.

protected String toQuery(Tree tree) {
    TreeVisitor visitor = new TreeVisitor();
    TreeToQuery treeToQuery = new TreeToQuery();
    visitor.visit(tree, treeToQuery);
    return treeToQuery.getQueryString().trim();
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) TreeToQuery(com.haulmont.cuba.core.sys.jpql.TreeToQuery)

Example 14 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.

the class QueryTreeAnalyzer method hasJoins.

public boolean hasJoins() {
    CommonTree sourceNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
    List<SelectionSourceNode> selectionSourceNodes = getChildrenByClass(sourceNode, SelectionSourceNode.class);
    if (selectionSourceNodes.size() > 1) {
        return true;
    } else if (selectionSourceNodes.size() == 1) {
        NodesFinder<JoinVariableNode> nodesFinder = new NodesFinder<>(JoinVariableNode.class);
        TreeVisitor treeVisitor = new TreeVisitor();
        treeVisitor.visit(tree, nodesFinder);
        return !nodesFinder.getFoundNodes().isEmpty();
    } else {
        return false;
    }
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) NodesFinder(com.haulmont.cuba.core.sys.jpql.transform.NodesFinder) CommonTree(org.antlr.runtime.tree.CommonTree)

Example 15 with TreeVisitor

use of org.antlr.runtime.tree.TreeVisitor 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)

Aggregations

TreeVisitor (org.antlr.runtime.tree.TreeVisitor)20 TreeVisitorAction (org.antlr.runtime.tree.TreeVisitorAction)8 CommonTree (org.antlr.runtime.tree.CommonTree)4 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)2 NodesFinder (com.haulmont.cuba.core.sys.jpql.transform.NodesFinder)2 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)1 JPA2Lexer (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Lexer)1 JPA2Parser (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2Parser)1 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)1 ParameterCounter (com.haulmont.cuba.core.sys.jpql.transform.ParameterCounter)1 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)1 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)1 JoinVariableNode (com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode)1 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1