Search in sources :

Example 6 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory in project blaze-persistence by Blazebit.

the class Mappers method forViewConvertToViewAttributeMapping.

public static <S, T> Mapper<S, T> forViewConvertToViewAttributeMapping(EntityViewManagerImpl evm, ViewType<S> sourceViewType, ViewType<T> targetViewType, Map<String, String> mapping, Mapper<S, T> additionalMapper) {
    List<Mapper<S, T>> mappers = new ArrayList<>();
    ExpressionFactory ef = evm.getCriteriaBuilderFactory().getService(ExpressionFactory.class);
    for (MethodAttribute<?, ?> attribute : targetViewType.getAttributes()) {
        if (attribute.isUpdatable() && attribute instanceof MappingAttribute<?, ?> && attribute instanceof SingularAttribute<?, ?>) {
            for (Map.Entry<String, String> entry : mapping.entrySet()) {
                if (entry.getValue().equals(((MappingAttribute) attribute).getMapping())) {
                    Type<?> attributeType = ((SingularAttribute<?, ?>) attribute).getType();
                    AttributeAccessor entityAccessor;
                    if (entry.getKey().isEmpty()) {
                        entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), ((MappingAttribute<?, ?>) sourceViewType.getIdAttribute()).getMapping());
                    } else {
                        entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), entry.getKey());
                    }
                    AttributeAccessor targetAttributeAccessor = Accessors.forMutableViewAttribute(evm, attribute);
                    if (attributeType instanceof ViewType<?>) {
                        ViewType<?> viewType = (ViewType<?>) attributeType;
                        Type<?> attributeViewIdType = ((SingularAttribute<?, ?>) viewType.getIdAttribute()).getType();
                        EntityTupleizer entityTupleizer = null;
                        ObjectBuilder<?> idViewBuilder = null;
                        if (attributeViewIdType instanceof ManagedViewType<?>) {
                            entityTupleizer = new DefaultEntityTupleizer(evm, (ManagedViewType<?>) attributeViewIdType);
                            idViewBuilder = (ObjectBuilder<Object>) evm.getTemplate(new MacroConfigurationExpressionFactory(ef, ef.getDefaultMacroConfiguration()), (ManagedViewTypeImplementor<?>) attributeViewIdType, null, null, new MutableViewJpqlMacro(), null, new MutableEmbeddingViewJpqlMacro(), 0).createObjectBuilder(null, null, null, 0, false, false);
                        }
                        mappers.add(new ReferenceViewAttributeMapper<S, T>(evm, entityAccessor, viewType.getJavaType(), entityTupleizer, targetAttributeAccessor, idViewBuilder));
                    } else {
                        mappers.add((Mapper<S, T>) new AttributeMapper<>(Collections.singletonList(entityAccessor), Collections.singletonList(targetAttributeAccessor)));
                    }
                }
            }
        }
    }
    if (mappers.isEmpty()) {
        return additionalMapper;
    }
    return new CompositeMapper<>(mappers.toArray(new Mapper[mappers.size()]));
}
Also used : DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) EntityTupleizer(com.blazebit.persistence.view.impl.entity.EntityTupleizer) ArrayList(java.util.ArrayList) MappingAttribute(com.blazebit.persistence.view.metamodel.MappingAttribute) MutableEmbeddingViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro) DefaultEntityTupleizer(com.blazebit.persistence.view.impl.update.DefaultEntityTupleizer) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) MutableViewJpqlMacro(com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro) ManagedViewTypeImplementor(com.blazebit.persistence.view.impl.metamodel.ManagedViewTypeImplementor) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) SingularAttribute(com.blazebit.persistence.view.metamodel.SingularAttribute) AttributeAccessor(com.blazebit.persistence.view.impl.accessor.AttributeAccessor) HashMap(java.util.HashMap) Map(java.util.Map) ManagedViewType(com.blazebit.persistence.view.metamodel.ManagedViewType) ViewType(com.blazebit.persistence.view.metamodel.ViewType)

Example 7 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory in project blaze-persistence by Blazebit.

the class SimpleCachingExpressionFactoryTest method testCreateSimpleExpressionCache.

@Test
public void testCreateSimpleExpressionCache() {
    ExpressionFactory ef = new SimpleCachingExpressionFactory(new ExpressionFactoryImpl(new HashMap<String, FunctionKind>(), true, true));
    String expressionString = "SIZE(Hello.world[:hahaha].criteria[1].api.lsls[a.b.c.d.e]) + SIZE(Hello.world[:hahaha].criteria[1].api.lsls[a.b.c.d.e])";
    Expression expr1 = ef.createSimpleExpression(expressionString, false, true, false, null, null);
    Expression expr2 = ef.createSimpleExpression(expressionString, false, true, false, null, null);
    Assert.assertFalse(expr1 == expr2);
    Assert.assertEquals(expr1, expr2);
}
Also used : ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) HashMap(java.util.HashMap) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Expression(com.blazebit.persistence.parser.expression.Expression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) ExpressionFactoryImpl(com.blazebit.persistence.parser.expression.ExpressionFactoryImpl) Test(org.junit.Test)

Example 8 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory 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);
}
Also used : ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) HashMap(java.util.HashMap) SimpleCachingExpressionFactory(com.blazebit.persistence.parser.expression.SimpleCachingExpressionFactory) ExpressionFactoryImpl(com.blazebit.persistence.parser.expression.ExpressionFactoryImpl) MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Expression(com.blazebit.persistence.parser.expression.Expression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PathExpression(com.blazebit.persistence.parser.expression.PathExpression) PathElementExpression(com.blazebit.persistence.parser.expression.PathElementExpression) ArrayExpression(com.blazebit.persistence.parser.expression.ArrayExpression) PropertyExpression(com.blazebit.persistence.parser.expression.PropertyExpression) Test(org.junit.Test)

Example 9 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory 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);
}
Also used : MacroFunction(com.blazebit.persistence.parser.expression.MacroFunction) MacroConfiguration(com.blazebit.persistence.parser.expression.MacroConfiguration) AbstractCachingExpressionFactory(com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory) ExpressionFactory(com.blazebit.persistence.parser.expression.ExpressionFactory) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory) FunctionPassthroughJpqlMacro(com.blazebit.persistence.view.impl.macro.FunctionPassthroughJpqlMacro) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JpqlMacroAdapter(com.blazebit.persistence.view.impl.JpqlMacroAdapter) MacroConfigurationExpressionFactory(com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)

Example 10 with ExpressionFactory

use of com.blazebit.persistence.parser.expression.ExpressionFactory 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

ExpressionFactory (com.blazebit.persistence.parser.expression.ExpressionFactory)18 HashMap (java.util.HashMap)8 MutableEmbeddingViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableEmbeddingViewJpqlMacro)5 MutableViewJpqlMacro (com.blazebit.persistence.view.impl.macro.MutableViewJpqlMacro)5 Expression (com.blazebit.persistence.parser.expression.Expression)4 MacroConfiguration (com.blazebit.persistence.parser.expression.MacroConfiguration)4 MacroFunction (com.blazebit.persistence.parser.expression.MacroFunction)4 PathExpression (com.blazebit.persistence.parser.expression.PathExpression)4 PropertyExpression (com.blazebit.persistence.parser.expression.PropertyExpression)4 MacroConfigurationExpressionFactory (com.blazebit.persistence.view.impl.MacroConfigurationExpressionFactory)4 ManagedViewType (com.blazebit.persistence.view.metamodel.ManagedViewType)4 ViewType (com.blazebit.persistence.view.metamodel.ViewType)4 AbstractCachingExpressionFactory (com.blazebit.persistence.parser.expression.AbstractCachingExpressionFactory)3 Map (java.util.Map)3 ArrayExpression (com.blazebit.persistence.parser.expression.ArrayExpression)2 ExpressionFactoryImpl (com.blazebit.persistence.parser.expression.ExpressionFactoryImpl)2 ListIndexExpression (com.blazebit.persistence.parser.expression.ListIndexExpression)2 MapKeyExpression (com.blazebit.persistence.parser.expression.MapKeyExpression)2 NullExpression (com.blazebit.persistence.parser.expression.NullExpression)2 ParameterExpression (com.blazebit.persistence.parser.expression.ParameterExpression)2