Search in sources :

Example 16 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method addJoinAsId.

@Test
public void addJoinAsId() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$Constraint u, sec$GroupHierarchy h where h.userGroup = :par");
    transformer.addJoinAsIs("join a.parent.constraints c");
    String res = transformer.getResult();
    assertEquals("select h from sec$Constraint u, sec$GroupHierarchy h join a.parent.constraints c where h.userGroup = :par", 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 17 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testRemoveDistinct.

@Test
public void testRemoveDistinct() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select distinct h from sec$GroupHierarchy h");
    transformer.removeDistinct();
    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 18 with QueryTransformerAstBased

use of com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased 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 19 with QueryTransformerAstBased

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

the class QueryTransformerAstSoftDeleteBugsTest method testAddWhereDeleteTs.

// #PL-1998 (fixed)
public void testAddWhereDeleteTs() throws Exception {
    DomainModel model = createDomainModel();
    QueryTransformerAstBased transformer = createTransformer("select u from sec$User u where u.active = true or u.active = true");
    transformer.addWhere("{E}.deleteTs is null");
    String res = transformer.getResult();
    assertEquals("select u from sec$User u where (u.active = true or u.active = true) and (u.deleteTs is null)", res);
    transformer = createTransformer("select u from sec$User u where u.active = true");
    transformer.addWhere("{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where (u.active = true) and (u.deleteTs is null)", res);
    transformer = createTransformer("select u from sec$User u where (u.active = true)");
    transformer.addWhere("{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where ((u.active = true)) and (u.deleteTs is null)", res);
    transformer = createTransformer("select u from sec$User u where u.active = true and u.active = true");
    transformer.addWhere("{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where (u.active = true and u.active = true) and (u.deleteTs is null)", res);
    transformer = createTransformer("select u from sec$User u");
    transformer.addWhere("{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where u.deleteTs is null", res);
    // addJoinAndWhere
    transformer = createTransformer("select u from sec$User u where u.active = true or u.active = true");
    transformer.addJoinAndWhere("join u.group g", "{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u join u.group g where (u.active = true or u.active = true) and (u.deleteTs is null)", res);
    transformer = createTransformer("select u from sec$User u where (u.active = true)");
    transformer.addJoinAndWhere("join u.group g", "{E}.deleteTs is null");
    res = transformer.getResult();
    assertEquals("select u from sec$User u join u.group g where ((u.active = true)) and (u.deleteTs is null)", res);
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)

Example 20 with QueryTransformerAstBased

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

the class QueryParserAstBasedTest method testNestedEntityGroupBy.

@Test
public void testNestedEntityGroupBy() throws Exception {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c.group, count(c.id) from sec$Constraint c group by c.group");
    transformer.replaceWithSelectEntityVariable("tempEntityAlias");
    transformer.addFirstSelectionSource(String.format("%s tempEntityAlias", "sec$Group"));
    transformer.addWhereAsIs(String.format("tempEntityAlias.id = %s.id", "c.group"));
    transformer.addEntityInGroupBy("tempEntityAlias");
    System.out.println(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)

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