use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method prepareDomainModel.
private DomainModel prepareDomainModel() {
EntityBuilder builder = new EntityBuilder();
builder.startNewEntity("sec$GroupHierarchy");
builder.addStringAttribute("group");
builder.addStringAttribute("createdBy");
builder.addReferenceAttribute("parent", "sec$GroupHierarchy");
builder.addReferenceAttribute("other", "sec$GroupHierarchy");
builder.addReferenceAttribute("token", "fake$EmbeddedToken", "token", true);
builder.addCollectionReferenceAttribute("constraints", "sec$Constraint");
JpqlEntityModel groupHierarchy = builder.produce();
builder = new EntityBuilder();
builder.startNewEntity("sec$Constraint");
builder.addReferenceAttribute("group", "sec$GroupHierarchy");
JpqlEntityModel constraintEntity = builder.produce();
JpqlEntityModel userEntity = builder.produceImmediately("sec$User", "login");
builder = new EntityBuilder();
builder.startNewEntity("fake$EmbeddedToken");
builder.addStringAttribute("name");
builder.addStringAttribute("code");
builder.addReferenceAttribute("manager", "sec$User");
builder.addReferenceAttribute("parentToken", "fake$EmbeddedToken", "parentToken", true);
JpqlEntityModel token = builder.produce();
return new DomainModel(groupHierarchy, constraintEntity, userEntity, token);
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method testAddWhereWithSubquery.
@Test
public void testAddWhereWithSubquery() throws Exception {
DomainModel model = prepareDomainModel();
QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select u from sec$User u");
transformer.addWhere("{E}.login not like '[hide]'");
transformer.addWhere("{E}.group.id in (select h.group.id from sec$GroupHierarchy h " + "where h.group.id = :session$userGroupId or h.parent.id = :session$userGroupId)");
String res = transformer.getResult();
assertEquals("select u from sec$User u " + "where (u.login not like '[hide]') " + "and (u.group.id in (" + "select h.group.id from sec$GroupHierarchy h " + "where h.group.id = :session$userGroupId or h.parent.id = :session$userGroupId))", res);
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method test.
@Test
public void test() throws RecognitionException {
DomainModel model = prepareDomainModel();
QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.userGroup = :par");
transformer.addWhere("{E}.createdBy = :par1");
String res = transformer.getResult();
assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = :par) and (h.createdBy = :par1)", res);
transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.userGroup = ?1 " + "group by c.level having c.level > 0 order by c.level");
transformer.addWhere("{E}.createdBy = :par1");
res = transformer.getResult();
assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = ?1) " + "and (h.createdBy = :par1) group by c.level having c.level > 0 order by c.level", res);
Set<String> set = transformer.getAddedParams();
assertEquals(1, set.size());
assertEquals("par1", set.iterator().next());
transformer.addWhere("({E}.updatedBy = :par2 and {E}.groupId = :par3)");
res = transformer.getResult();
assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where ((h.userGroup = ?1) " + "and (h.createdBy = :par1)) and ((h.updatedBy = :par2 and h.groupId = :par3)) group by c.level having c.level > 0 order by c.level", res);
set = transformer.getAddedParams();
assertEquals(3, set.size());
transformer.reset();
transformer.mergeWhere("select gh from sec$GroupHierarchy gh where gh.version between 1 and 2");
res = transformer.getResult();
assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.userGroup = ?1) " + "and (h.version between 1 and 2) group by c.level having c.level > 0 order by c.level", res);
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method getResult_noChangesMade_withParameters.
@Test
public void getResult_noChangesMade_withParameters() throws RecognitionException {
EntityBuilder builder = new EntityBuilder();
JpqlEntityModel playerEntity = builder.produceImmediately("Player", "name", "nickname");
DomainModel model = new DomainModel(playerEntity);
assertTransformsToSame(model, "select p.nickname from Player p where p.name = :name");
assertTransformsToSame(model, "select p.nickname from Player p where p.name = :name or p.name = :name2");
assertTransformsToSame(model, "select p.nickname from Player p where p.name = ?1 or p.name = ?2");
assertTransformsToSame(model, "select p.nickname from Player p where p.name like :component$playersFilter.name52981");
}
use of com.haulmont.cuba.core.sys.jpql.DomainModel in project cuba by cuba-platform.
the class QueryTransformerAstBasedTest method testAliasPlaceholder.
@Test
public void testAliasPlaceholder() throws RecognitionException {
DomainModel model = prepareDomainModel();
QueryTransformerAstBased transformer = new QueryTransformerAstBased(model, "select c from sec$GroupHierarchy h join h.parent.constraints c where h.group = :par");
transformer.addWhere("{E}.createdBy = :par1 and {E}.updatedBy = :par2");
String res = transformer.getResult();
assertEquals("select c from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2)", res);
// //////////////////////////////////
transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
transformer.addJoinAndWhere("join h.parent.constraints c", "{E}.createdBy = :par1 and {E}.updatedBy = :par2 and c.createTs = :par3");
res = transformer.getResult();
assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2 and c.createTs = :par3)", res);
// //////////////////////////////////
transformer = new QueryTransformerAstBased(model, "select h from sec$GroupHierarchy h where h.group = :par");
transformer.addJoinAndWhere("join {E}.parent.constraints c", "{E}.createdBy = :par1 and {E}.updatedBy = :par2 and c.createTs = :par3");
res = transformer.getResult();
assertEquals("select h from sec$GroupHierarchy h join h.parent.constraints c where (h.group = :par) and (h.createdBy = :par1" + " and h.updatedBy = :par2 and c.createTs = :par3)", res);
}
Aggregations