Search in sources :

Example 16 with DomainModel

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);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) CommonTree(org.antlr.runtime.tree.CommonTree) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) QueryTreeTransformer(com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer) VariableEntityReference(com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference) Test(org.junit.Test)

Example 17 with DomainModel

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());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) Test(org.junit.Test)

Example 18 with DomainModel

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());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) Test(org.junit.Test)

Example 19 with DomainModel

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"));
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) Test(org.junit.Test)

Example 20 with DomainModel

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());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) 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