Search in sources :

Example 1 with JpqlMacro

use of com.blazebit.persistence.spi.JpqlMacro in project blaze-persistence by Blazebit.

the class SubqueryTest method testSubqueryCorrelatesMacro.

@Test
public // Test for #421
void testSubqueryCorrelatesMacro() {
    CriteriaBuilder<Document> crit = cbf.create(em, Document.class, "d");
    crit.registerMacro("test", new JpqlMacro() {

        @Override
        public void render(FunctionRenderContext context) {
            context.addChunk("d.");
            context.addArgument(0);
        }
    });
    crit.where("owner").in().from("TEST(people)").where("partnerDocument").eqExpression("d").end();
    String peopleCorrelation = correlationPath(Document.class, "d.people", "people", "id = d.id");
    String where = " WHERE ";
    String peopleCorrelationWhere = "";
    int idx;
    if ((idx = peopleCorrelation.indexOf(where)) != -1) {
        peopleCorrelationWhere = peopleCorrelation.substring(idx + where.length());
        peopleCorrelation = peopleCorrelation.substring(0, idx);
    }
    String expectedQuery = "SELECT d FROM Document d WHERE d.owner IN (SELECT people FROM " + peopleCorrelation + " WHERE ";
    if (!peopleCorrelationWhere.isEmpty()) {
        expectedQuery += peopleCorrelationWhere + " AND ";
    }
    expectedQuery += "people.partnerDocument = d)";
    assertEquals(expectedQuery, crit.getQueryString());
    crit.getResultList();
}
Also used : JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) Document(com.blazebit.persistence.testsuite.entity.Document) FunctionRenderContext(com.blazebit.persistence.spi.FunctionRenderContext) Test(org.junit.Test)

Example 2 with JpqlMacro

use of com.blazebit.persistence.spi.JpqlMacro in project blaze-persistence by Blazebit.

the class JpqlMacroTest method testScopedMacro.

@Test
public void testScopedMacro() {
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class, "d");
    verifyException(cb, SyntaxErrorException.class, r -> r.where("ID(d)"));
    cb.registerMacro("id", new JpqlMacro() {

        @Override
        public void render(FunctionRenderContext context) {
            context.addArgument(0);
            context.addChunk(".id");
        }
    });
    cb.where("ID(d)").eq(1);
    String expected = "SELECT d FROM Document d WHERE d.id = :param_0";
    assertEquals(expected, cb.getQueryString());
    CriteriaBuilder<Document> newCb = cbf.create(em, Document.class, "d");
    verifyException(newCb, SyntaxErrorException.class, r -> r.where("ID(d)"));
}
Also used : JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) Document(com.blazebit.persistence.testsuite.entity.Document) FunctionRenderContext(com.blazebit.persistence.spi.FunctionRenderContext) Test(org.junit.Test)

Example 3 with JpqlMacro

use of com.blazebit.persistence.spi.JpqlMacro in project blaze-persistence by Blazebit.

the class EntityViewManagerImpl method createObjectBuilder.

public ObjectBuilder<?> createObjectBuilder(ManagedViewTypeImplementor<?> viewType, MappingConstructorImpl<?> mappingConstructor, Class<?> rootType, String entityViewRoot, String embeddingViewPath, FullQueryBuilder<?, ?> criteriaBuilder, EntityViewConfiguration configuration, int offset, int suffix, boolean nullFlatViewIfEmpty) {
    ExpressionFactory ef = criteriaBuilder.getService(ExpressionFactory.class);
    if (!viewType.getEntityClass().isAssignableFrom(rootType)) {
        if (rootType.isAssignableFrom(viewType.getEntityClass())) {
            entityViewRoot = "TREAT(" + entityViewRoot + " AS " + metamodel.getEntityMetamodel().getEntity(viewType.getJavaType()).getName() + ")";
        } else {
            throw new IllegalArgumentException("The given view type with the entity type '" + viewType.getEntityClass().getName() + "' can not be applied to the query builder with result type '" + rootType.getName() + "'");
        }
    }
    MacroConfiguration originalMacroConfiguration = ef.getDefaultMacroConfiguration();
    ExpressionFactory cachingExpressionFactory = ef.unwrap(AbstractCachingExpressionFactory.class);
    JpqlMacro viewRootJpqlMacro = new DefaultViewRootJpqlMacro(entityViewRoot);
    ViewJpqlMacro viewJpqlMacro = configuration.getViewJpqlMacro();
    EmbeddingViewJpqlMacro embeddingViewJpqlMacro = configuration.getEmbeddingViewJpqlMacro();
    viewJpqlMacro.setViewPath(entityViewRoot);
    Map<String, MacroFunction> macros = new HashMap<>();
    macros.put("view", new JpqlMacroAdapter(viewJpqlMacro, cachingExpressionFactory));
    macros.put("view_root", new JpqlMacroAdapter(viewRootJpqlMacro, cachingExpressionFactory));
    macros.put("embedding_view", new JpqlMacroAdapter(embeddingViewJpqlMacro, cachingExpressionFactory));
    MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
    MacroConfigurationExpressionFactory macroEf = new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
    criteriaBuilder.registerMacro("view_root", viewRootJpqlMacro);
    return getTemplate(macroEf, viewType, mappingConstructor, entityViewRoot, viewJpqlMacro, embeddingViewPath, embeddingViewJpqlMacro, offset).createObjectBuilder(criteriaBuilder, configuration.getOptionalParameters(), configuration, suffix, false, nullFlatViewIfEmpty);
}
Also used : AbstractCachingExpressionFactory(com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) DefaultViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro) EmbeddingViewJpqlMacro(com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro) JpqlMacro(com.blazebit.persistence.spi.JpqlMacro) DefaultViewRootJpqlMacro(com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro) ViewJpqlMacro(com.blazebit.persistence.view.spi.ViewJpqlMacro) MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration)

Aggregations

JpqlMacro (com.blazebit.persistence.spi.JpqlMacro)3 FunctionRenderContext (com.blazebit.persistence.spi.FunctionRenderContext)2 Document (com.blazebit.persistence.testsuite.entity.Document)2 Test (org.junit.Test)2 AbstractCachingExpressionFactory (com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory)1 ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)1 MacroConfiguration (com.blazebit.persistence.parser.expression.MacroConfiguration)1 MacroFunction (com.blazebit.persistence.parser.expression.MacroFunction)1 DefaultViewRootJpqlMacro (com.blazebit.persistence.view.impl.macro.DefaultViewRootJpqlMacro)1 EmbeddingViewJpqlMacro (com.blazebit.persistence.view.spi.EmbeddingViewJpqlMacro)1 ViewJpqlMacro (com.blazebit.persistence.view.spi.ViewJpqlMacro)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1