Search in sources :

Example 1 with QueryTransformer

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();
    }
}
Also used : QueryTransformer(io.jmix.data.QueryTransformer)

Example 2 with QueryTransformer

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();
}
Also used : QueryParser(io.jmix.data.QueryParser) QueryTransformer(io.jmix.data.QueryTransformer)

Example 3 with QueryTransformer

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;
    }
}
Also used : QueryTransformer(io.jmix.data.QueryTransformer)

Example 4 with QueryTransformer

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;
}
Also used : JpqlCondition(io.jmix.core.querycondition.JpqlCondition) QueryTransformer(io.jmix.data.QueryTransformer)

Example 5 with QueryTransformer

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());
}
Also used : QueryParser(io.jmix.data.QueryParser) DomainModel(io.jmix.data.impl.jpql.DomainModel) QueryParserAstBased(io.jmix.data.impl.jpql.QueryParserAstBased) QueryTransformer(io.jmix.data.QueryTransformer) QueryTransformerAstBased(io.jmix.data.impl.jpql.transform.QueryTransformerAstBased) JpqlEntityModel(io.jmix.data.impl.jpql.model.JpqlEntityModel) Test(org.junit.jupiter.api.Test)

Aggregations

QueryTransformer (io.jmix.data.QueryTransformer)10 MetaClass (io.jmix.core.metamodel.model.MetaClass)2 QueryParser (io.jmix.data.QueryParser)2 Entity (io.jmix.core.Entity)1 JpqlCondition (io.jmix.core.querycondition.JpqlCondition)1 DomainModel (io.jmix.data.impl.jpql.DomainModel)1 QueryParserAstBased (io.jmix.data.impl.jpql.QueryParserAstBased)1 JpqlEntityModel (io.jmix.data.impl.jpql.model.JpqlEntityModel)1 QueryTransformerAstBased (io.jmix.data.impl.jpql.transform.QueryTransformerAstBased)1 BaseRole (io.jmix.security.model.BaseRole)1 Test (org.junit.jupiter.api.Test)1