use of com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference in project cuba by cuba-platform.
the class QueryAnalyzerTest method replaceWithCount_distinct.
@Test
public void replaceWithCount_distinct() throws RecognitionException {
DomainModel model = prepareDomainModel();
QueryTreeTransformer qa = new QueryTreeTransformer();
qa.prepare(model, "select distinct d from Car c, in(c.drivers) d order by d.name");
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("d", 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("Driver", "d"));
selectedItems = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEMS);
assertEquals(1, selectedItems.getChildCount());
selectedItem = selectedItems.getFirstChildWithType(JPA2Lexer.T_SELECTED_ITEM);
assertTrue(selectedItem.getChild(0) instanceof AggregateExpressionNode);
AggregateExpressionNode countExpr = (AggregateExpressionNode) selectedItem.getChild(0);
assertEquals("count", countExpr.getChild(0).getText());
assertEquals("distinct", countExpr.getChild(2).getText());
assertEquals("d", countExpr.getChild(3).getText());
assertEquals(5, countExpr.getChildCount());
assertNull(orderByNode.getFirstChildWithType(JPA2Lexer.T_ORDER_BY));
}
use of com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference in project cuba by cuba-platform.
the class QueryAnalyzerTest method mixinJoinIntoTree_with_in_collections.
@Test
public void mixinJoinIntoTree_with_in_collections() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
builder.startNewEntity("HomeBase");
builder.addStringAttribute("name");
JpqlEntityModel homeBase = builder.produce();
builder.startNewEntity("Driver");
builder.addStringAttribute("name");
builder.addStringAttribute("signal");
JpqlEntityModel driver = builder.produce();
builder.startNewEntity("Car");
builder.addStringAttribute("model");
builder.addCollectionReferenceAttribute("drivers", "Driver");
builder.addReferenceAttribute("station", "HomeBase");
JpqlEntityModel car = builder.produce();
DomainModel model = new DomainModel(car, driver, homeBase);
JoinVariableNode join = Parser.parseJoinClause("join c.station h").get(0);
QueryTreeTransformer qa = new QueryTreeTransformer();
qa.prepare(model, "select d.name from Car c, in(c.drivers) d");
CommonTree tree = qa.getTree();
CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(2, sources.getChildCount());
assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
CommonTree source0 = (CommonTree) sources.getChild(0);
assertEquals(1, source0.getChildCount());
assertTrue(source0.getChild(0) instanceof IdentificationVariableNode);
assertTrue(sources.getChild(1) instanceof SelectionSourceNode);
CommonTree source1 = (CommonTree) sources.getChild(1);
assertTrue(source1.getChild(0) instanceof CollectionMemberNode);
qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true);
tree = qa.getTree();
sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(2, sources.getChildCount());
assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
source0 = (CommonTree) sources.getChild(0);
assertEquals(2, source0.getChildCount());
assertTrue(source0.getChild(0) instanceof IdentificationVariableNode);
assertTrue(source0.getChild(1) instanceof JoinVariableNode);
assertTrue(sources.getChild(1) instanceof SelectionSourceNode);
source1 = (CommonTree) sources.getChild(1);
assertTrue(source1.getChild(0) instanceof CollectionMemberNode);
}
use of com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference 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());
}
use of com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference 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());
}
use of com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference 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));
}
Aggregations