Search in sources :

Example 26 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testReplaceWithCount.

@Test
public void testReplaceWithCount() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0 order by c.level");
    transformer.replaceWithCount();
    assertEquals("select count(c) from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0 order by c.level");
    transformer.replaceWithCount();
    assertEquals("select count(c) from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0", transformer.getResult());
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) Test(org.junit.Test)

Example 27 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withLeft_InnerJoinFetch.

@Test
public void getResult_noChangesMade_withLeft_InnerJoinFetch() 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();
    DomainModel model = new DomainModel(teamEntity, playerEntity, personEntity);
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "select p.name from Player p join fetch p.team left join fetch p.agent");
    assertEquals("select p.name from Player p join fetch p.team left join fetch p.agent", transformerAstBased.getResult());
    transformerAstBased = new QueryTransformerAstBased(model, "select p.name from Player p left outer join fetch p.team inner join fetch p.agent");
    assertEquals("select p.name from Player p left outer join fetch p.team inner join fetch p.agent", 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 28 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testReplaceWithCount_distinct.

@Test
public void testReplaceWithCount_distinct() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select distinct c from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0 order by c.level");
    transformer.replaceWithCount();
    String res = transformer.getResult();
    assertEquals("select count(distinct c) from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1 " + "group by c.level having c.level > 0", 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 29 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_withWhere.

@Test
public void getResult_noChangesMade_withWhere() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addReferenceAttribute("team", "Team");
    builder.addStringAttribute("name");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    assertTransformsToSame(model, "SELECT p FROM Player p where p.name = 'de Souza'");
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p left join p.team as t WHERE t.name = 'KS FC'");
    String result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p left join p.team t WHERE t.name = 'KS FC'", 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 30 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_orderBySeveralFields.

@Test
public void getResult_noChangesMade_orderBySeveralFields() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name");
    builder.startNewEntity("Player");
    builder.addSingleValueAttribute(Date.class, "birthDate");
    builder.addStringAttribute("surname");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "SELECT p FROM Player p order by p.birthDate, p.surname");
    String result = transformerAstBased.getResult();
    assertEquals("SELECT p FROM Player p order by p.birthDate, p.surname", 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)

Aggregations

QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)39 DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)37 Test (org.junit.Test)37 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)9 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)9 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)2