Search in sources :

Example 31 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testJoin_WithComma.

@Test
public void testJoin_WithComma() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
    transformer.addJoinAndWhere("join h.parent.constraints pco, sec$Constraint sc", "1 = 1");
    String res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints pco, sec$Constraint sc where (h.group = :par) and (1 = 1)", res);
    transformer.reset();
    transformer.addJoinAsIs("join h.parent.constraints pco, sec$Constraint sc");
    res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints pco, sec$Constraint sc where h.group = :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 32 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testAddWhereWithInExpression.

@Test
public void testAddWhereWithInExpression() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$Constraint c");
    transformer.addWhere("{E}.group in (select g.id from sec$GroupHierarchy g where {E} in (g.constraints))");
    assertEquals("select c from sec$Constraint c " + "where c.group in (select g.id from sec$GroupHierarchy g where c in ( g.constraints))", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "select c from sec$Constraint c");
    transformer.addWhere("{E}.group in (select g.id from sec$GroupHierarchy g where {E}.id in (g.constraints.id))");
    assertEquals("select c from sec$Constraint c " + "where c.group in (select g.id from sec$GroupHierarchy g where c.id in ( g.constraints.id))", 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 33 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testNotCorrectEntityAliasInWhere.

@Test
public void testNotCorrectEntityAliasInWhere() {
    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");
    // since 3.4 we don't check equality of targetEntity and an entity in the query
    transformer.addWhere("a.createdBy = :par1");
    assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.group = ?1) and (a.createdBy = :par1) " + "group by c.level having c.level > 0 order by c.level", 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 34 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method addWhere_with_child_select.

@Test
public void addWhere_with_child_select() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addReferenceAttribute("driver", "Person");
    JpqlEntityModel car = builder.produce();
    JpqlEntityModel person = builder.produceImmediately("Person", "fullname");
    DomainModel model = new DomainModel(car, person);
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select car.driver from Car car");
    transformer.addWhere("{E}.model = ?1");
    assertEquals("select car.driver from Car car where car.model = ?1", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "select car.driver from Car car");
    transformer.addWhere("{E}.driver.fullname = ?1");
    assertEquals("select car.driver from Car car where car.driver.fullname = ?1", transformer.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 35 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_delete.

@Test
public void getResult_noChangesMade_delete() throws Exception {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "delete from sec$GroupHierarchy g where g.createdBy = :createdBy");
    assertEquals(transformerAstBased.getResult(), "delete from sec$GroupHierarchy g where g.createdBy = :createdBy");
}
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