Search in sources :

Example 26 with DomainModel

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

the class QueryTransformerAstBasedTest method testDoubleJoins.

@Test
public void testDoubleJoins() throws Exception {
    DomainModel model = prepareDomainModel();
    QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
    transformer.reset();
    transformer.addJoinAndWhere("join h.parent.constraints c1 join h.constraints c2", "c.createdBy = :par2");
    String res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c1 join h.constraints c2 where (h.group = :par) and (c.createdBy = :par2)", res);
    transformer.reset();
    transformer.addJoinAndWhere("join {E}.parent p join p.constraints cr", "c.createdBy = :par2");
    res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent p join p.constraints cr where (h.group = :par) and (c.createdBy = :par2)", res);
    transformer.reset();
    transformer.addJoinAndWhere("join replaceEntity.parent p join p.constraints cr", "c.createdBy = :par2");
    res = transformer.getResult();
    assertEquals("select h from sec$GroupHierarchy h join h.parent p join p.constraints cr where (h.group = :par) and (c.createdBy = :par2)", 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 27 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_with_in_collections.

@Test
public void getResult_noChangesMade_with_in_collections() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel driver = builder.produceImmediately("Driver", "name", "signal");
    builder.startNewEntity("HomeBase");
    builder.addStringAttribute("city");
    JpqlEntityModel homeBase = builder.produce();
    builder.startNewEntity("Car");
    builder.addStringAttribute("model");
    builder.addCollectionReferenceAttribute("drivers", "Driver");
    builder.addReferenceAttribute("station", "HomeBase");
    JpqlEntityModel car = builder.produce();
    DomainModel model = new DomainModel(car, driver, homeBase);
    assertTransformsToSame(model, "select d.name from Car c, in(c.drivers) d where d.name = ?1");
    assertTransformsToSame(model, "select d.name from Car c join c.station s, in(c.drivers) d where d.name = ?1 and s.city = :par2");
}
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 28 with DomainModel

use of com.haulmont.cuba.core.sys.jpql.DomainModel 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 29 with DomainModel

use of com.haulmont.cuba.core.sys.jpql.DomainModel 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 30 with DomainModel

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

Aggregations

DomainModel (com.haulmont.cuba.core.sys.jpql.DomainModel)94 Test (org.junit.Test)86 EntityBuilder (com.haulmont.cuba.core.sys.jpql.model.EntityBuilder)49 JpqlEntityModel (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModel)45 QueryTransformerAstBased (com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased)37 HintProvider (com.haulmont.cuba.gui.components.autocomplete.impl.HintProvider)21 HintResponse (com.haulmont.cuba.gui.components.autocomplete.impl.HintResponse)20 QueryTreeTransformer (com.haulmont.cuba.core.sys.jpql.transform.QueryTreeTransformer)8 CommonTree (org.antlr.runtime.tree.CommonTree)7 VariableEntityReference (com.haulmont.cuba.core.sys.jpql.transform.VariableEntityReference)5 JpqlEntityModelImpl (com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl)4 JpqlSyntaxException (com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException)3 Tree (org.antlr.runtime.tree.Tree)3 DomainModelBuilder (com.haulmont.cuba.core.sys.jpql.DomainModelBuilder)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 TreeToQuery (com.haulmont.cuba.core.sys.jpql.TreeToQuery)1 PathEntityReference (com.haulmont.cuba.core.sys.jpql.transform.PathEntityReference)1 HintRequest (com.haulmont.cuba.gui.components.autocomplete.impl.HintRequest)1 Option (com.haulmont.cuba.gui.components.autocomplete.impl.Option)1 ArrayList (java.util.ArrayList)1