Search in sources :

Example 6 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method assertTransformsToSame.

private void assertTransformsToSame(DomainModel model, String query) throws RecognitionException {
    QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, query);
    String result = transformerAstBased.getResult();
    assertEquals(query, result);
}
Also used : QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)

Example 7 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method transformationsUsingSelectedEntity.

@Test
public void transformationsUsingSelectedEntity() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("sec$Car");
    builder.addStringAttribute("model");
    builder.addReferenceAttribute("colour", "sec$Colour");
    JpqlEntityModel car = builder.produce();
    builder.startNewEntity("sec$Colour");
    builder.addStringAttribute("name");
    builder.addStringAttribute("createdBy");
    builder.addSingleValueAttribute(Integer.class, "version");
    builder.addReferenceAttribute("manufacturer", "Manufacturer");
    JpqlEntityModel colour = builder.produce();
    JpqlEntityModel manufacturer = builder.produceImmediately("Manufacturer", "companyName");
    DomainModel model = new DomainModel(car, colour, manufacturer);
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c.colour from sec$Car c where c.colour.createdBy = :p");
    transformer.addWhere("{E}.colour.createdBy = :session$userLogin");
    String res = transformer.getResult();
    assertEquals("select c.colour from sec$Car c where (c.colour.createdBy = :p) and (c.colour.createdBy = :session$userLogin)", res);
    transformer = new QueryTransformerAstBased(model, "select c.colour from sec$Car c where c.colour.createdBy = :p");
    transformer.addJoinAndWhere("join {E}.manufacturer m", "m.companyName = :par1");
    res = transformer.getResult();
    assertEquals("select c.colour from sec$Car c join c.manufacturer m where (c.colour.createdBy = :p) and (m.companyName = :par1)", res);
// todo eude: fix the following
// transformer = new QueryTransformerAstBased(model,
// "select c.colour from sec$Car c where c.colour.createdBy = :p");
// transformer.mergeWhere("select gh from sec$Colour gh where gh.version between 1 and 2");
// 
// res = transformer.getResult();
// assertEquals(
// "select c.colour from sec$Car c where (c.colour.createdBy = :p) and (c.colour.version between 1 and 2)",
// res);
// transformer = new QueryTransformerAstBased(model,
// "select c.colour from sec$Car c where c.colour.createdBy = :p order by c.colour.name");
// transformer.replaceOrderBy(true, "version");
// 
// res = transformer.getResult();
// assertEquals(
// "select c.colour from sec$Car c left join c.colour c_colour where c.colour.createdBy = :p order by c_colour.version desc",
// res);
// 
// 
// transformer = new QueryTransformerAstBased(model,
// "select c.colour from sec$Car c where c.colour.createdBy = :p");
// transformer.replaceWithCount();
// 
// res = transformer.getResult();
// assertEquals(
// "select count(c.colour) from sec$Car c where c.colour.createdBy = :p",
// res);
}
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 8 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_update.

@Test
public void getResult_noChangesMade_update() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    builder.startNewEntity("sec$Car");
    builder.addStringAttribute("model");
    builder.addStringAttribute("vin");
    JpqlEntityModel car = builder.produce();
    DomainModel model = new DomainModel(car);
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "update sec$Car c set c.model = :model, c.vin = :vin");
    assertEquals("update sec$Car c set c.model=:model,c.vin=:vin", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "update sec$Car c set c.model = :model, c.vin = :vin where c.vin = :oldVin");
    assertEquals("update sec$Car c set c.model=:model,c.vin=:vin where c.vin = :oldVin", 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 9 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method addWhere_adds_Where_IfNeeded.

// todo eude : fix the following
// @Test
// public void testInvalidEntity() 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");
// try {
// transformer.addWhere("a.createdBy = :par1");
// fail();
// } catch (Exception e) {
// assertTrue(e instanceof RuntimeException);
// }
// }
@Test
public void addWhere_adds_Where_IfNeeded() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h");
    transformer.addWhere("h.group = ?1");
    assertEquals("select h from sec$GroupHierarchy h where h.group = ?1", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h join h.parent.constraints c");
    transformer.addWhere("h.group = ?1");
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c where h.group = ?1", transformer.getResult());
    transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h join h.parent.constraints c group by c.level having c.level > 0 order by c.level");
    transformer.addWhere("h.group = ?1");
    assertEquals("select h 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.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 10 with QueryTransformerAstBased

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

the class QueryTransformerAstBasedTest method testReplaceInCondition.

@Test
public void testReplaceInCondition() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select u from sec$User u where u.login in (:param)");
    transformer.replaceInCondition("param");
    String res = transformer.getResult();
    assertEquals("select u from sec$User u where 1=0", res);
    transformer = new QueryTransformerAstBased(model, "select u from sec$User u where u.login not in (:param)");
    transformer.replaceInCondition("param");
    res = transformer.getResult();
    assertEquals("select u from sec$User u where 1=1", res);
}
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