Search in sources :

Example 31 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade.

@Test
public void getResult_noChangesMade() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModelImpl playerEntity = builder.produceImmediately("Player");
    DomainModel model = new DomainModel(playerEntity);
    assertTransformsToSame(model, "SELECT p FROM Player p");
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlEntityModelImpl(com.haulmont.cuba.core.sys.jpql.model.JpqlEntityModelImpl) EntityBuilder(com.haulmont.cuba.core.sys.jpql.model.EntityBuilder) Test(org.junit.Test)

Example 32 with DomainModel

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

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

Example 34 with DomainModel

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

the class QueryTransformerAstBasedTest method getResult_noChangesMade_subqueries.

// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void getResult_noChangesMade_withSubqueries() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// builder.startNewEntity("Player");
// builder.addStringAttribute("name");
// builder.addStringAttribute("nickname");
// builder.addReferenceAttribute("team", "Team");
// JpqlEntityModel playerEntity = builder.produce();
// 
// DomainModel model = new DomainModel(playerEntity, teamEntity);
// 
// assertTransformsToSame(model, "select p.name from (select t.name from Team t) p");
// assertTransformsToSame(model, "select p.owner from (select t.name, t.owner from Team t) p");
// assertTransformsToSame(model, "select g.owner from (select t.name, t.owner from Team t) p, (select t.name from Team t) g");
// }
// The following functions are not supported by JPA2 JPQL (see jpql21.bnf)
// @Test
// public void getResult_noChangesMade_severalLevelsOfSubquery() throws RecognitionException {
// EntityBuilder builder = new EntityBuilder();
// JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
// 
// builder.startNewEntity("Player");
// builder.addStringAttribute("name");
// builder.addStringAttribute("nickname");
// builder.addReferenceAttribute("team", "Team");
// JpqlEntityModel playerEntity = builder.produce();
// 
// DomainModel model = new DomainModel(playerEntity, teamEntity);
// 
// assertTransformsToSame(model, "select p.owner from (select t from Team t where t.name in (select a.name from Player a)) p");
// // 'as' skipped
// QueryTransformerAstBased transformerAstBased = new QueryTransformerAstBased(model, "select p.owner from (select t.owner from Team as t where t.name = '1') p", "AsdfAsdfAsdf");
// String result = transformerAstBased.getResult();
// assertEquals("select p.owner from (select t.owner from Team t where t.name = '1') p", result);
// }
@Test
public void getResult_noChangesMade_subqueries() throws RecognitionException {
    EntityBuilder builder = new EntityBuilder();
    JpqlEntityModel teamEntity = builder.produceImmediately("Team", "name", "owner");
    builder.startNewEntity("Player");
    builder.addStringAttribute("name");
    builder.addStringAttribute("nickname");
    builder.addReferenceAttribute("team", "Team");
    JpqlEntityModel playerEntity = builder.produce();
    DomainModel model = new DomainModel(playerEntity, teamEntity);
    assertTransformsToSame(model, "select p.owner from Player pl where exists (select t from Team t where t.name = 'Team1' and pl.team.id = t.id)");
    assertTransformsToSame(model, "select p.owner from Player pl where pl.team.id in (select t.id from Team t where t.name = 'Team1')");
    assertTransformsToSame(model, "select t.name from Team t where (select count(p) from Player p where p.team.id = t.id) > 10");
}
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 35 with DomainModel

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

the class QueryTransformerAstBasedTest method addWhere_onIncorrectQuery.

// todo eude : fix the following
// @Test
// public void addWhere_onIncorrectHavingInTheEnd() throws RecognitionException {
// DomainModel model = prepareDomainModel();
// 
// try {
// new QueryTransformerAstBased(model,
// "select h from sec$GroupHierarchy h join h.parent.constraints c group by c.level order by c.level having c.level > 0");
// fail("Incorrectly placed 'having' passed");
// } catch (JpqlSyntaxException e) {
// //expected
// }
// }
@Test
public void addWhere_onIncorrectQuery() throws RecognitionException {
    DomainModel model = prepareDomainModel();
    try {
        QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h join h.parent.constraints");
        transformer.getResult();
        fail("Not named join variable passed");
    } catch (JpqlSyntaxException e) {
    // expected
    }
}
Also used : DomainModel(com.haulmont.cuba.core.sys.jpql.DomainModel) JpqlSyntaxException(com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException) QueryTransformerAstBased(com.haulmont.cuba.core.sys.jpql.transform.QueryTransformerAstBased) 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