Search in sources :

Example 91 with DomainModel

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

the class QueryAnalyzerTest method mixinWhereConditionsIntoTree.

@Test
public void mixinWhereConditionsIntoTree() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select c from Car c");
    WhereNode where = (WhereNode) Parser.parseWhereClause("where c.model = ?1");
    CommonTree tree = qa.getTree();
    assertNull(tree.getFirstChildWithType(JPA2Lexer.T_CONDITION));
    qa.mixinWhereConditionsIntoTree(where);
    tree = qa.getTree();
    assertNotNull(tree.getFirstChildWithType(JPA2Lexer.T_CONDITION));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) Test(org.junit.Test)

Example 92 with DomainModel

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

Example 93 with DomainModel

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

the class QueryAnalyzerTest method mixinJoinIntoTree.

@Test
public void mixinJoinIntoTree() 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 a.drivers d").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);
    assertEquals("d", joinNode.getVariableName());
    assertEquals("c", ((PathNode) join.getChild(0)).getEntityVariableName());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Example 94 with DomainModel

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

the class QueryAnalyzerTest method replaceWithCount.

@Test
public void replaceWithCount() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTreeTransformer qa = new QueryTreeTransformer();
    qa.prepare(model, "select c from Car c order by c.model");
    CommonTree tree = qa.getTree();
    CommonTree selectedItems = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS);
    Tree selectedItem = selectedItems.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEM);
    PathNode pathNode = (PathNode) selectedItem.getChild(0);
    assertEquals("c", pathNode.getEntityVariableName());
    assertEquals(0, pathNode.getChildCount());
    CommonTree orderByNode = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_ORDER_BY);
    assertNotNull(orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY_FIELD));
    qa.replaceWithCount(new VariableEntityReference("Car", "c"));
    assertTrue(selectedItem.getChild(0) instanceof AggregateExpressionNode);
    AggregateExpressionNode countExpr = (AggregateExpressionNode) selectedItem.getChild(0);
    assertEquals("count", countExpr.getChild(0).getText());
    assertEquals("c", countExpr.getChild(2).getText());
    assertEquals(4, countExpr.getChildCount());
    assertNull(orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Aggregations

DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)94 Test (org.junit.Test)86 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)49 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)45 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)37 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)21 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)20 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)8 CommonTree (org.antlr.runtime.tree.CommonTree)7 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)5 JpqlEntityModelImpl (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl)4 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)3 Tree (org.antlr.runtime.tree.Tree)3 DomainModelBuilder (com.haulmont.cuba.core.sys.jpql.DomainModelBuilder)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)1 PathEntityReference (com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference)1 HintRequest (com.haulmont.cuba.gui.components.autocomplete.impl.HintRequest)1 Option (com.haulmont.cuba.gui.components.autocomplete.impl.Option)1 ArrayList (java.util.ArrayList)1