Search in sources :

Example 36 with DomainModel

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

the class QueryTransformerAstBasedTest method testAddWrongWhere.

@Test
public void testAddWrongWhere() throws RecognitionException {
    try {
        DomainModel model = prepareDomainModel();
        QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$Constraint c");
        transformer.addWhere("{E}.group.id == :group");
        fail();
    } catch (JpqlSyntaxException e) {
    // expected
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 37 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withAggregateExpression.

@Test
public void getResult_noChangesMade_withAggregateExpression() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addSingleValueAttribute(Integer.class, "age");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    assertTransformsToSame(model, "SELECT count(p) FROM Player p");
    assertTransformsToSame(model, "SELECT max(p.age) FROM Player p");
    assertTransformsToSame(model, "SELECT min(p.age) FROM Player p");
    assertTransformsToSame(model, "SELECT avg(p.age) FROM Player p");
    assertTransformsToSame(model, "SELECT avg(p.age)*4.1 FROM Player p");
    assertTransformsToSame(model, "SELECT sum(p.age) FROM Player p");
    assertTransformsToSame(model, "SELECT max(p.age), t FROM Player p join p.team t group by t");
    assertTransformsToSame(model, "SELECT max(p.age), t.name FROM Player p join p.team t group by t.name");
}
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 38 with DomainModel

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

the class QueryTransformerAstBasedTest method testRemoveOrderBy.

@Test
public void testRemoveOrderBy() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h order by h.createdBy");
    transformer.removeOrderBy();
    String res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h", 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 39 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withJoin.

@Test
public void getResult_noChangesMade_withJoin() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel personEntity = builder.produceImmediately("Person", "personName");
    builder.startNewEntity("Team");
    builder.addStringAttribute("name");
    builder.addStringAttribute("owner");
    builder.addReferenceAttribute("manager", "Person");
    JpqlEntityModel teamEntity = builder.produce();
    builder.startNewEntity("Player");
    builder.addStringAttribute("name");
    builder.addStringAttribute("nickname");
    builder.addReferenceAttribute("team", "Team");
    builder.addReferenceAttribute("agent", "Person");
    JpqlEntityModel playerEntity = builder.produce();
    builder.startNewEntity("League");
    builder.addStringAttribute("name");
    builder.addCollectionReferenceAttribute("teams", "Team");
    JpqlEntityModel leagueEntity = builder.produce();
    builder.startNewEntity("Country");
    builder.addStringAttribute("flag");
    builder.addReferenceAttribute("league", "League");
    JpqlEntityModel countryEntity = builder.produce();
    DomainModel model = new DomainModel(teamEntity, playerEntity, leagueEntity, personEntity, countryEntity);
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "select m.personName from Player p left join p.team.manager as m");
    String result = transformerAstBased.getResult();
    assertEquals("select m.personName from Player p left join p.team.manager m", result);
    transformerAstBased = new QueryTransformerAstBased(model, "select c.flag from Country c join c.league.teams as t join t.manager m");
    assertEquals("select c.flag from Country c join c.league.teams t join t.manager m", transformerAstBased.getResult());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) JpqlEntityModel(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel) Test(org.junit.Test)

Example 40 with DomainModel

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

the class QueryTransformerAstBasedTest method testHandleCaseInsensitiveParam.

@Test
public void testHandleCaseInsensitiveParam() throws Exception {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select u from sec$User u where u.name like :name");
    transformer.handleCaseInsensitiveParam("name");
    String res = transformer.getResult();
    assertEquals("select u from sec$User u where lower ( u.name) like :name", res);
    transformer = new QueryTransformerAstBased(model, "select u from sec$User u where u.name=:name");
    transformer.handleCaseInsensitiveParam("name");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where lower ( u.name) = :name", res);
    transformer = new QueryTransformerAstBased(model, "select u from sec$User u where concat(u.name, ' ', u.login) = :name");
    transformer.handleCaseInsensitiveParam("name");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where concat( lower ( u.name), ' ', lower ( u.login)) = :name", 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