Search in sources :

Example 21 with EntityBuilder

use of com.haulmont.cuba.core.sys.jpql.model.EntityBuilder 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 22 with EntityBuilder

use of com.haulmont.cuba.core.sys.jpql.model.EntityBuilder 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 23 with EntityBuilder

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_AsInSelect.

@Test
public void getResult_noChangesMade_AsInSelect() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel playerEntity = builder.produceImmediately("Player", "name", "nickname", "level");
    DomainModel model = new DomainModel(playerEntity);
    assertTransformsToSame(model, "select p.name as name, p.nickname as nickname from Player p");
    assertTransformsToSame(model, "select p.name as name from Player p");
}
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 24 with EntityBuilder

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withMacros.

@Test
public void getResult_noChangesMade_withMacros() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addSingleValueAttribute(Date.class, "birthDate");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @between(p.birthDate, now-2, now+2, month) ");
    String result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @between ( p.birthDate, now - 2, now + 2, month)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateBefore(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateBefore ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateAfter(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateAfter ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @dateEquals(p.birthDate, :d, USER_TIMEZONE) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @dateEquals ( p.birthDate, :d, USER_TIMEZONE)", result);
    transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p where @today(p.birthDate) ");
    result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p where @today ( p.birthDate)", result);
}
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 25 with EntityBuilder

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

the class QueryTransformationTest 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.addCollectionReferenceAttribute("constraints", "sec$Constraint");
    JpqlEntityModel groupHierarchy = builder.produce();
    JpqlEntityModel constraintEntity = builder.produceImmediately("sec$Constraint");
    return new DomainModel(groupHierarchy, constraintEntity);
}
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)

Aggregations

EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)50 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)49 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)46 Test (org.junit.Test)44 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)20 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)19 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)9 JpqlEntityModelImpl (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl)4 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)1 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)1 CommonTree (org.antlr.runtime.tree.CommonTree)1