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");
}
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());
}
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);
}
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");
}
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
}
}
Aggregations