use of com.haulmont.cuba.core.sys.jpql.DomainModel 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.DomainModel in project cuba by cuba-platform.
the class QueryParserAstBasedTest method testMainEntity.
@Test
public void testMainEntity() throws Exception {
DomainModel model = prepareDomainModel();
QueryParserAstBased parser = new QueryParserAstBased(model, "select h from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$GroupHierarchy", parser.getEntityName());
assertEquals("h", parser.getEntityAlias());
parser = new QueryParserAstBased(model, "select h.group from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$GroupHierarchy", parser.getEntityName());
assertEquals("h", parser.getEntityAlias());
parser = new QueryParserAstBased(model, "select u from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$Constraint", parser.getEntityName());
assertEquals("u", parser.getEntityAlias());
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryParserAstBasedTest method testGetNestedEntityNameIfNestedSelected.
@Test
public void testGetNestedEntityNameIfNestedSelected() throws Exception {
DomainModel model = prepareDomainModel();
QueryParserAstBased parser = new QueryParserAstBased(model, "select h.group from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$Group", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("h.group", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select g.group from sec$GroupHierarchy h join h.group g");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select g from sec$GroupHierarchy h join fetch h.group g");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select h.parent.other from sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$GroupHierarchy", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("h.parent.other", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select h.parent.other.group from sec$GroupHierarchy h where h.userGroup = :par");
assertEquals("sec$Group", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("h.parent.other.group", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select g from sec$GroupHierarchy h join h.group g where h.userGroup = :par");
assertEquals("sec$Group", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("g", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select p from sec$GroupHierarchy h join h.parent p g where h.userGroup = :par");
assertEquals("sec$GroupHierarchy", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("p", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select h from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select h.parent.other.createdBy from sec$GroupHierarchy h where h.userGroup = :par");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select c from sec$GroupHierarchy h, sec$Constraint c where h.userGroup = :par");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select c.group from sec$GroupHierarchy h, sec$Constraint c where h.userGroup = :par");
assertEquals("sec$GroupHierarchy", parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertEquals("c.group", parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
parser = new QueryParserAstBased(model, "select u.group, u.login from sec$User u where u.name like :mask");
assertNull(parser.getEntityNameIfSecondaryReturnedInsteadOfMain());
assertNull(parser.getEntityPathIfSecondaryReturnedInsteadOfMain());
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryParserAstBasedTest method testHasIsNullCondition.
@Test
public void testHasIsNullCondition() throws Exception {
DomainModel model = prepareDomainModel();
QueryParserAstBased parser = new QueryParserAstBased(model, "select c from ref$Car c");
assertFalse(parser.hasIsNullCondition("colour"));
parser = new QueryParserAstBased(model, "select c from ref$Car c where c.colour = ?1");
assertFalse(parser.hasIsNullCondition("colour"));
parser = new QueryParserAstBased(model, "select c from ref$Car c where c.colour is null");
assertTrue(parser.hasIsNullCondition("colour"));
parser = new QueryParserAstBased(model, "select c from ref$Car c where c.model.manufacturer is null");
assertTrue(parser.hasIsNullCondition("model.manufacturer"));
parser = new QueryParserAstBased(model, "select c from ref$Car c where c.model = (select a from ref$Other a where a.model is null)");
assertFalse(parser.hasIsNullCondition("model"));
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryParserAstBasedTest method testHasJoins.
@Test
public void testHasJoins() throws Exception {
DomainModel model = prepareDomainModel();
QueryParserAstBased parser = new QueryParserAstBased(model, "select h.group from sec$Constraint u, sec$GroupHierarchy h");
assertTrue(parser.hasJoins());
parser = new QueryParserAstBased(model, "select g.group from sec$GroupHierarchy h join h.group g");
assertTrue(parser.hasJoins());
parser = new QueryParserAstBased(model, "select h.parent.other from sec$GroupHierarchy h");
assertFalse(parser.hasJoins());
}
Aggregations