use of com.blazebit.persistence.parser.expression.MacroFunction in project blaze-persistence by Blazebit.
the class SimpleCachingExpressionFactoryTest method testCreateSimpleExpressionCacheWithMacros.
@Test
public void testCreateSimpleExpressionCacheWithMacros() {
ExpressionFactory ef = new SimpleCachingExpressionFactory(new ExpressionFactoryImpl(new HashMap<String, FunctionKind>(), true, true));
MacroConfiguration macroConfiguration = MacroConfiguration.of(Collections.singletonMap("my_macro", (MacroFunction) new MacroFunction() {
@Override
public Expression apply(List<Expression> expressions) {
PathExpression p;
if (expressions.get(0) instanceof PathExpression) {
p = (PathExpression) expressions.get(0);
} else {
p = new PathExpression(new ArrayList<>(Arrays.asList((PathElementExpression) expressions.get(0))));
}
p.getExpressions().add(new ArrayExpression(new PropertyExpression("lsls"), new PathExpression(Arrays.<PathElementExpression>asList(new PropertyExpression("a"), new PropertyExpression("b"), new PropertyExpression("c"), new PropertyExpression("d"), new PropertyExpression("e")))));
return p;
}
@Override
public Object[] getState() {
return new Object[0];
}
@Override
public boolean supportsCaching() {
return true;
}
@Override
public int hashCode() {
return getClass().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj.getClass() == getClass();
}
}));
String expressionString = "SIZE(my_macro(Hello.world[:hahaha].criteria[1].api)) + SIZE(my_macro(Hello.world[:hahaha].criteria[1].api))";
Expression expr1 = ef.createSimpleExpression(expressionString, false, true, false, macroConfiguration, null);
Expression expr2 = ef.createSimpleExpression(expressionString, false, true, false, macroConfiguration, null);
Assert.assertFalse(expr1 == expr2);
Assert.assertEquals(expr1, expr2);
}
use of com.blazebit.persistence.parser.expression.MacroFunction in project blaze-persistence by Blazebit.
the class MetamodelBuildingContextImpl method createTypeValidationExpressionFactory.
private MacroConfigurationExpressionFactory createTypeValidationExpressionFactory() {
MacroConfiguration originalMacroConfiguration = expressionFactory.getDefaultMacroConfiguration();
ExpressionFactory cachingExpressionFactory = expressionFactory.unwrap(AbstractCachingExpressionFactory.class);
Map<String, MacroFunction> macros = new HashMap<>();
macros.put("view", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("VIEW"), cachingExpressionFactory));
macros.put("view_root", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("VIEW_ROOT"), cachingExpressionFactory));
macros.put("embedding_view", new JpqlMacroAdapter(new FunctionPassthroughJpqlMacro("EMBEDDING_VIEW"), cachingExpressionFactory));
MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
return new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
}
use of com.blazebit.persistence.parser.expression.MacroFunction 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);
}
use of com.blazebit.persistence.parser.expression.MacroFunction in project blaze-persistence by Blazebit.
the class MetamodelBuildingContextImpl method createMacroAwareExpressionFactory.
@Override
public MacroConfigurationExpressionFactory createMacroAwareExpressionFactory(String viewRoot) {
MacroConfiguration originalMacroConfiguration = expressionFactory.getDefaultMacroConfiguration();
ExpressionFactory cachingExpressionFactory = expressionFactory.unwrap(AbstractCachingExpressionFactory.class);
Map<String, MacroFunction> macros = new HashMap<>();
macros.put("view", new JpqlMacroAdapter(new MutableViewJpqlMacro(viewRoot), cachingExpressionFactory));
macros.put("view_root", new JpqlMacroAdapter(new DefaultViewRootJpqlMacro(viewRoot), cachingExpressionFactory));
macros.put("embedding_view", new JpqlMacroAdapter(new MutableEmbeddingViewJpqlMacro(), cachingExpressionFactory));
MacroConfiguration macroConfiguration = originalMacroConfiguration.with(macros);
return new MacroConfigurationExpressionFactory(cachingExpressionFactory, macroConfiguration);
}
use of com.blazebit.persistence.parser.expression.MacroFunction in project blaze-persistence by Blazebit.
the class PrefixingQueryGenerator method visit.
@Override
public void visit(FunctionExpression expression) {
String macroName;
List<Expression> expressions = expression.getExpressions();
if (ExpressionUtils.isCustomFunctionInvocation(expression) && MACROS.contains((macroName = ((StringLiteral) expressions.get(0)).getValue()).toUpperCase())) {
if (expandMacros) {
MacroFunction macroFunction = expressionFactory.getDefaultMacroConfiguration().get(macroName);
Expression expandedExpression = macroFunction.apply(expressions.subList(1, expressions.size()));
// It makes no sense to prefix a path that was created through a view macro as that is already absolute
boolean doPrefix = this.doPrefix;
try {
this.doPrefix = false;
expandedExpression.accept(this);
} finally {
this.doPrefix = doPrefix;
}
} else {
// When invoking this in the embedding view scope, we have to redirect VIEW to EMBEDDING_VIEW and unregister EMBEDDING_VIEW
if (fromEmbeddingViewScope) {
if ("EMBEDDING_VIEW".equalsIgnoreCase(macroName)) {
throw new IllegalArgumentException("Illegal use of EMBEDDING_VIEW!");
} else if ("VIEW".equalsIgnoreCase(macroName)) {
sb.append("EMBEDDING_VIEW");
} else {
sb.append(macroName);
}
} else {
sb.append(macroName);
}
sb.append('(');
int size = expressions.size();
if (size > 1) {
expressions.get(1).accept(this);
for (int i = 2; i < size; i++) {
sb.append(",");
expressions.get(i).accept(this);
}
}
sb.append(')');
}
} else {
super.visit(expression);
}
}
Aggregations