Search in sources :

Example 21 with JpqlEntityModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withMultiEntitySelect.

@Test
public void getResult_noChangesMade_withMultiEntitySelect() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team");
    JpqlEntityModel playerEntity = builder.produceImmediately("Player");
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    assertTransformsToSame(model, "SELECT p, t FROM Player p, Team t");
}
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 JpqlEntityModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_subqueries.

// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void getResult_noChangesMade_withSubqueries() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// builder.startNewEntity("Player");
// builder.addStringAttribute("name");
// builder.addStringAttribute("nickname");
// builder.addReferenceAttribute("team", "Team");
// JpqlEntityModel playerEntity = builder.produce();
// 
// DomainModel model = new DomainModel(playerEntity, teamEntity);
// 
// assertTransformsToSame(model, "select p.name from (select t.name from Team t) p");
// assertTransformsToSame(model, "select p.owner from (select t.name, t.owner from Team t) p");
// assertTransformsToSame(model, "select g.owner from (select t.name, t.owner from Team t) p, (select t.name from Team t) g");
// }
// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void getResult_noChangesMade_severalLevelsOfSubquery() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// builder.startNewEntity("Player");
// builder.addStringAttribute("name");
// builder.addStringAttribute("nickname");
// builder.addReferenceAttribute("team", "Team");
// JpqlEntityModel playerEntity = builder.produce();
// 
// DomainModel model = new DomainModel(playerEntity, teamEntity);
// 
// assertTransformsToSame(model, "select p.owner from (select t from Team t where t.name in (select a.name from Player a)) p");
// // 'as' skipped
// QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "select p.owner from (select t.owner from Team as t where t.name = '1') p", "AsdfAsdfAsdf");
// String result = transformerAstBased.getResult();
// assertEquals("select p.owner from (select t.owner from Team t where t.name = '1') p", result);
// }
@Test
public void getResult_noChangesMade_subqueries() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
    builder.startNewEntity("Player");
    builder.addStringAttribute("name");
    builder.addStringAttribute("nickname");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    assertTransformsToSame(model, "select p.owner from Player pl where exists (select t from Team t where t.name = 'Team1' and pl.team.id = t.id)");
    assertTransformsToSame(model, "select p.owner from Player pl where pl.team.id in (select t.id from Team t where t.name = 'Team1')");
    assertTransformsToSame(model, "select t.name from Team t where (select count(p) from Player p where p.team.id = t.id) > 10");
}
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 23 with JpqlEntityModel

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

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

use of com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel 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)

Aggregations

JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)57 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)46 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)45 Test (org.junit.Test)40 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)17 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)16 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)9 ArrayList (java.util.ArrayList)5 Attribute (com.haulmont.cuba.core.sys.jpql.model.Attribute)4 PathNode (com.haulmont.cuba.core.sys.jpql.tree.PathNode)4 IdentificationVariableNode (com.haulmont.cuba.core.sys.jpql.tree.IdentificationVariableNode)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 UnknownEntityNameException (com.haulmont.cuba.core.sys.jpql.UnknownEntityNameException)2 NoJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.NoJpqlEntityModel)2 Pointer (com.haulmont.cuba.core.sys.jpql.pointer.Pointer)2 List (java.util.List)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 JPA2RecognitionException (com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)1 VirtualJpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.VirtualJpqlEntityModel)1