use of io.jmix.data.QueryTransformer in project jmix by jmix-framework.
the class JpqlQueryBuilder method applyCount.
protected void applyCount() {
if (countQuery) {
QueryTransformer transformer = queryTransformerFactory.transformer(resultQuery);
transformer.replaceWithCount();
resultQuery = transformer.getResult();
}
}
use of io.jmix.data.QueryTransformer in project jmix by jmix-framework.
the class ConditionJpqlGenerator method processQuery.
/**
* Returns a JPQL query modified according to the given tree of conditions.
*
* @param query JPQL query
* @param context condition generation context
* @return a JPQL query modified according to the given tree of conditions
*/
public String processQuery(String query, ConditionGenerationContext context) {
if (context.getCondition() == null) {
return query;
}
QueryTransformer transformer = queryTransformerFactory.transformer(query);
QueryParser parser = queryTransformerFactory.parser(query);
context.setEntityAlias(parser.getEntityAlias());
if (context.getValueProperties() != null) {
context.setSelectedExpressions(parser.getSelectedExpressionsList());
}
copyGenerationContext(context);
String joins = generateJoins(context);
String where = generateWhere(context);
if (!Strings.isNullOrEmpty(joins)) {
transformer.addJoinAndWhere(joins, where);
} else {
transformer.addWhere(where);
}
return transformer.getResult();
}
use of io.jmix.data.QueryTransformer in project jmix by jmix-framework.
the class SortJpqlGenerator method transformQuery.
protected String transformQuery(String queryString, Map<String, Sort.Direction> sortExpressions, Sort.Direction direction) {
if (!sortExpressions.isEmpty()) {
QueryTransformer transformer = queryTransformerFactory.transformer(queryString);
transformer.replaceOrderByExpressions(sortExpressions);
return transformer.getResult();
} else {
return queryString;
}
}
use of io.jmix.data.QueryTransformer in project jmix by jmix-framework.
the class QueryFilter method processQuery.
public String processQuery(String query, Map<String, Object> paramValues) {
Set<String> params = new HashSet<>();
for (Map.Entry<String, Object> entry : paramValues.entrySet()) {
if (paramValueIsOk(entry.getValue()))
params.add(entry.getKey());
}
query = TemplateHelper.processTemplate(query, paramValues);
if (isActual(root, params)) {
Condition refined = refine(root, params);
if (refined != null) {
QueryTransformer transformer = queryTransformerFactory.transformer(query);
String where = filterJpqlGenerator.generateJpql(refined);
if (!StringUtils.isBlank(where)) {
Set<String> joins = refined.getJoins();
if (!joins.isEmpty()) {
joins.forEach(transformer::addJoin);
}
transformer.addWhere(where);
}
return transformer.getResult();
}
}
return query;
}
use of io.jmix.data.QueryTransformer in project jmix by jmix-framework.
the class QueryTransformerAstBasedTest method testTreatSupport.
@Test
public void testTreatSupport() {
JpqlEntityModel parent = EntityBuilder.create().startNewEntity("test$Parent").addReferenceAttribute("child", "test$Child").produce();
JpqlEntityModel child = EntityBuilder.create().produceImmediately("test$Child", "name");
JpqlEntityModel extChild = EntityBuilder.create().startNewEntity("test$ExtChild").addStringAttribute("extName").addReferenceAttribute("subChild", "test$SubExtChild").produce();
JpqlEntityModel subExtChild = EntityBuilder.create().produceImmediately("test$SubExtChild", "title");
DomainModel model = new DomainModel(parent, child, extChild, subExtChild);
QueryTransformer transformer = new QueryTransformerAstBased(model, "select p from test$Parent p join treat(p.child as test$ExtChild) e");
transformer.addWhere("e.extName is null");
String result = transformer.getResult();
assertEquals("select p from test$Parent p join treat(p.child as test$ExtChild) e where e.extName is null", result);
String complicatedQuery = "select p,e,s from test$Parent p join treat(p.child as test$ExtChild) e join e.subChild s " + "where(e.extName is null and s.title like :someParam)";
QueryParser parser = new QueryParserAstBased(model, complicatedQuery);
List<QueryParser.QueryPath> paths = parser.getQueryPaths();
assertEquals(paths.get(0).getEntityName(), parent.getName());
assertEquals(paths.get(1).getEntityName(), extChild.getName());
assertEquals(paths.get(2).getEntityName(), subExtChild.getName());
}
Aggregations