Search in sources :

Example 1 with TreeToQuery

use of com.haulmont.cuba.core.sys.jpql.TreeToQuery 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 2 with TreeToQuery

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

the class QueryAnalyzerTest method mixinJoinOnIntoTree.

@Test
public void mixinJoinOnIntoTree() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select c from Car c");
    CommonTree tree = qa.getTree();
    CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
    assertEquals(1, sources.getChildCount());
    assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
    CommonTree source = (CommonTree) sources.getFirstChildWithType(JPA2Lexer.T_SOURCE);
    assertTrue(source.getChild(0) instanceof IdentificationVariableNode);
    JoinVariableNode join = Parser.parseJoinClause("join Driver d on d.car.id = c.id").get(0);
    qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true);
    tree = qa.getTree();
    sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
    assertEquals(1, sources.getChildCount());
    SelectionSourceNode sourceNode = (SelectionSourceNode) sources.getChild(0);
    assertEquals(2, sourceNode.getChildCount());
    assertTrue(sourceNode.getChild(0) instanceof IdentificationVariableNode);
    assertTrue(sourceNode.getChild(1) instanceof JoinVariableNode);
    JoinVariableNode joinNode = (JoinVariableNode) sourceNode.getChild(1);
    TreeVisitor visitor = new TreeVisitor();
    TreeToQuery treeToQuery = new TreeToQuery();
    visitor.visit(joinNode, treeToQuery);
    assertEquals("d", joinNode.getVariableName());
    assertEquals("join Driver d on d.car.id = c.id", treeToQuery.getQueryString().trim());
}
Also used : TreeVisitor(org.antlr.runtime.tree.TreeVisitor) DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) TreeToQuery(com.haulmont.cuba.core.sys.jpql.TreeToQuery) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Aggregations

TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)2 TreeVisitor (org.antlr.runtime.tree.TreeVisitor)2 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)1 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)1 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)1 CommonTree (org.antlr.runtime.tree.CommonTree)1 Test (org.junit.Test)1