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