Search in sources :

Example 21 with DomainModel

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

the class QueryTransformerAstBasedTest method prepareDomainModel.

private DomainModel prepareDomainModel() {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("sec$GroupHierarchy");
    builder.addStringAttribute("group");
    builder.addStringAttribute("createdBy");
    builder.addReferenceAttribute("parent", "sec$GroupHierarchy");
    builder.addReferenceAttribute("other", "sec$GroupHierarchy");
    builder.addReferenceAttribute("token", "fake$EmbeddedToken", "token", true);
    builder.addCollectionReferenceAttribute("constraints", "sec$Constraint");
    JpqlEntityModel groupHierarchy = builder.produce();
    builder = new EntityBuilder();
    builder.startNewEntity("sec$Constraint");
    builder.addReferenceAttribute("group", "sec$GroupHierarchy");
    JpqlEntityModel constraintEntity = builder.produce();
    JpqlEntityModel userEntity = builder.produceImmediately("sec$User", "login");
    builder = new EntityBuilder();
    builder.startNewEntity("fake$EmbeddedToken");
    builder.addStringAttribute("name");
    builder.addStringAttribute("code");
    builder.addReferenceAttribute("manager", "sec$User");
    builder.addReferenceAttribute("parentToken", "fake$EmbeddedToken", "parentToken", true);
    JpqlEntityModel token = builder.produce();
    return new DomainModel(groupHierarchy, constraintEntity, userEntity, token);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)

Example 22 with DomainModel

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

the class QueryTransformerAstBasedTest method testAddWhereWithSubquery.

@Test
public void testAddWhereWithSubquery() throws Exception {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select u from sec$User u");
    transformer.addWhere("{E}.login not like '[hide]'");
    transformer.addWhere("{E}.group.id in (select h.group.id from sec$GroupHierarchy h " + "where h.group.id = :session$userGroupId or h.parent.id = :session$userGroupId)");
    String res = transformer.getResult();
    assertEquals("select u from sec$User u " + "where (u.login not like '[hide]') " + "and (u.group.id in (" + "select h.group.id from sec$GroupHierarchy h " + "where h.group.id = :session$userGroupId or h.parent.id = :session$userGroupId))", res);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 23 with DomainModel

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

the class QueryTransformerAstBasedTest method test.

@Test
public void test() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.userGroup = :par");
    transformer.addWhere("{E}.createdBy = :par1");
    String res = transformer.getResult();
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = :par) and (h.createdBy = :par1)", res);
    transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.userGroup = ?1 " + "group by c.level having c.level > 0 order by c.level");
    transformer.addWhere("{E}.createdBy = :par1");
    res = transformer.getResult();
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = ?1) " + "and (h.createdBy = :par1) group by c.level having c.level > 0 order by c.level", res);
    Set<String> set = transformer.getAddedParams();
    assertEquals(1, set.size());
    assertEquals("par1", set.iterator().next());
    transformer.addWhere("({E}.updatedBy = :par2 and {E}.groupId = :par3)");
    res = transformer.getResult();
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where ((h.userGroup = ?1) " + "and (h.createdBy = :par1)) and ((h.updatedBy = :par2 and h.groupId = :par3)) group by c.level having c.level > 0 order by c.level", res);
    set = transformer.getAddedParams();
    assertEquals(3, set.size());
    transformer.reset();
    transformer.mergeWhere("select gh from sec$GroupHierarchy gh where gh.version between 1 and 2");
    res = transformer.getResult();
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = ?1) " + "and (h.version between 1 and 2) group by c.level having c.level > 0 order by c.level", res);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 24 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withParameters.

@Test
public void getResult_noChangesMade_withParameters() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel playerEntity = builder.produceImmediately("Player", "name", "nickname");
    DomainModel model = new DomainModel(playerEntity);
    assertTransformsToSame(model, "select p.nickname from Player p where p.name = :name");
    assertTransformsToSame(model, "select p.nickname from Player p where p.name = :name or p.name = :name2");
    assertTransformsToSame(model, "select p.nickname from Player p where p.name = ?1 or p.name = ?2");
    assertTransformsToSame(model, "select p.nickname from Player p where p.name like :component$playersFilter.name52981");
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Test(org.junit.Test)

Example 25 with DomainModel

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

the class QueryTransformerAstBasedTest method testAliasPlaceholder.

@Test
public void testAliasPlaceholder() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.group = :par");
    transformer.addWhere("{E}.createdBy = :par1 and {E}.updatedBy = :par2");
    String res = transformer.getResult();
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2)", res);
    // //////////////////////////////////
    transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
    transformer.addJoinAndWhere("join h.parent.constraints c", "{E}.createdBy = :par1 and {E}.updatedBy = :par2 and c.createTs = :par3");
    res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2 and c.createTs = :par3)", res);
    // //////////////////////////////////
    transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
    transformer.addJoinAndWhere("join {E}.parent.constraints c", "{E}.createdBy = :par1 and {E}.updatedBy = :par2 and c.createTs = :par3");
    res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2 and c.createTs = :par3)", res);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) 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