Search in sources :

Example 1 with ClassMapping

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping in project legend-engine by finos.

the class RelationalCompilerExtension method getExtraClassMappingFirstPassProcessors.

@Override
public List<Function3<ClassMapping, Mapping, CompileContext, Pair<SetImplementation, RichIterable<EmbeddedSetImplementation>>>> getExtraClassMappingFirstPassProcessors() {
    return Collections.singletonList((cm, parentMapping, context) -> {
        if (cm instanceof RootRelationalClassMapping) {
            RootRelationalClassMapping classMapping = (RootRelationalClassMapping) cm;
            String id = classMapping.id != null ? classMapping.id : getElementFullPath(context.resolveClass(classMapping._class, classMapping.classSourceInformation), context.pureModel.getExecutionSupport()).replaceAll("::", "_");
            final RootRelationalInstanceSetImplementation res = new Root_meta_relational_mapping_RootRelationalInstanceSetImplementation_Impl(id)._id(id);
            MutableList<RelationalOperationElement> groupByColumns = ListIterate.collect(classMapping.groupBy, relationalOperationElement -> HelperRelationalBuilder.processRelationalOperationElement(relationalOperationElement, context, Maps.mutable.empty(), Lists.mutable.empty()));
            org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.TableAlias mainTableAlias = null;
            // user has defined main table
            if (classMapping.mainTable != null) {
                Relation pureTable = HelperRelationalBuilder.getRelation(classMapping.mainTable, context);
                mainTableAlias = new Root_meta_relational_metamodel_TableAlias_Impl("")._relationalElement(pureTable)._database(HelperRelationalBuilder.resolveDatabase(classMapping.mainTable.getDb(), classMapping.mainTable.sourceInformation, context));
            }
            res._distinct(classMapping.distinct)._superSetImplementationId(classMapping.extendsClassMappingId)._root(classMapping.root)._mainTableAlias(mainTableAlias)._parent(parentMapping);
            if (classMapping.mappingClass != null) {
                res._mappingClass(HelperMappingBuilder.processMappingClass(classMapping.mappingClass, context, parentMapping));
            }
            if (!classMapping.groupBy.isEmpty()) {
                res._groupBy(new Root_meta_relational_mapping_GroupByMapping_Impl("")._columns(groupByColumns));
            }
            MutableList<org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation> embeddedRelationalPropertyMappings = Lists.mutable.empty();
            MutableMap<String, TableAlias> tableAliasesMap = Maps.mutable.empty();
            HelperRelationalBuilder.processRelationalClassMapping(classMapping, context, res, res, parentMapping, embeddedRelationalPropertyMappings, HelperMappingBuilder.getAllEnumerationMappings(parentMapping), tableAliasesMap);
            // user has not defined mainTable and the processing of mainTableAlias is complete (done with processing of class mapping)
            if (res._mainTableAlias() == null) {
                MutableSet<TableAlias> tableAliases = tableAliasesMap.valuesView().toSet();
                MutableSet<RelationalOperationElement> tables = tableAliases.collect(AliasAccessor::_relationalElement);
                MutableSet<org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.Database> databases = tableAliases.collect(TableAliasAccessor::_database);
                // if classMapping is extending another class the main table can be resolved in ExtraClassMappingSecondPassProcessors
                if ((tables.size() == 0 && classMapping.extendsClassMappingId == null) || tables.size() > 1) {
                    throw new EngineException("Can't find the main table for class '" + classMapping.id + "'. Please specify a main table using the ~mainTable directive.");
                }
                if ((databases.size() == 0 && classMapping.extendsClassMappingId == null) || databases.size() > 1) {
                    throw new EngineException("Can't find the main table for class '" + classMapping.id + "'. Inconsistent database definitions for the mapping");
                }
                if (tables.size() == 1 && databases.size() == 1) {
                    mainTableAlias = new Root_meta_relational_metamodel_TableAlias_Impl("");
                    mainTableAlias._relationalElement(tables.toList().getFirst());
                    mainTableAlias._database(databases.toList().getFirst());
                    res._mainTableAlias(mainTableAlias);
                    HelperRelationalBuilder.enhanceEmbeddedMappingsWithRelationalOperationElement(embeddedRelationalPropertyMappings, res);
                }
            }
            parentMapping._classMappingsAddAll(embeddedRelationalPropertyMappings);
            embeddedRelationalPropertyMappings.addAll(HelperRelationalBuilder.generateMilestoningRangeEmbeddedPropertyMapping(res, res, context));
            return Tuples.pair(res, Lists.immutable.empty());
        }
        return null;
    });
}
Also used : org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) EmbeddedRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation) Relation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) Database(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database) RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)

Example 2 with ClassMapping

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping in project legend-engine by finos.

the class RelationalCompilerExtension method getExtraAggregationAwareClassMappingSecondPassProcessors.

@Override
public List<Procedure3<AggregationAwareClassMapping, Mapping, CompileContext>> getExtraAggregationAwareClassMappingSecondPassProcessors() {
    return Collections.singletonList((cm, parentMapping, context) -> {
        AggregationAwareSetImplementation asi = (AggregationAwareSetImplementation) parentMapping._classMappings().detect(c -> HelperRelationalBuilder.getClassMappingId(c).equals(HelperMappingBuilder.getClassMappingId(cm, context)));
        if (cm.mainSetImplementation instanceof RootRelationalClassMapping) {
            RootRelationalClassMapping classMapping = (RootRelationalClassMapping) cm.mainSetImplementation;
            HelperRelationalBuilder.processRootRelationalClassMapping((RootRelationalInstanceSetImplementation) asi._mainSetImplementation(), classMapping, context);
        }
        for (AggregateSetImplementationContainer agg : cm.aggregateSetImplementations) {
            if (agg.setImplementation instanceof RootRelationalClassMapping) {
                RootRelationalClassMapping classMapping = (RootRelationalClassMapping) agg.setImplementation;
                asi._aggregateSetImplementations().forEach(c -> {
                    if (HelperRelationalBuilder.getClassMappingId(c._setImplementation()).equals(HelperMappingBuilder.getClassMappingId(classMapping, context))) {
                        HelperRelationalBuilder.processRootRelationalClassMapping((RootRelationalInstanceSetImplementation) c._setImplementation(), classMapping, context);
                    }
                });
            }
        }
    });
}
Also used : PostProcessorWithParameter(org.finos.legend.pure.m3.coreinstance.meta.relational.runtime.PostProcessorWithParameter) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) Multimap(org.eclipse.collections.api.multimap.Multimap) Schema(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Schema) RelationalAssociationImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RelationalAssociationImplementation) MutableList(org.eclipse.collections.api.list.MutableList) AggregationAwareClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregationAwareClassMapping) Maps(org.eclipse.collections.api.factory.Maps) RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) MutableSet(org.eclipse.collections.api.set.MutableSet) Handlers(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.Handlers) RichIterable(org.eclipse.collections.api.RichIterable) Store(org.finos.legend.pure.m3.coreinstance.meta.pure.store.Store) Relation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation) Processor(org.finos.legend.engine.language.pure.compiler.toPureGraph.extension.Processor) Tuples(org.eclipse.collections.impl.tuple.Tuples) RelationalInputData(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.mappingTest.RelationalInputData) org.finos.legend.pure.m3.coreinstance.meta.pure.mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping) Function4(org.finos.legend.engine.shared.core.function.Function4) IntObjectToIntFunction(org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction) PostProcessor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.postprocessor.PostProcessor) PureClientVersions(org.finos.legend.engine.protocol.pure.PureClientVersions) Objects(java.util.Objects) Procedure3(org.finos.legend.engine.shared.core.function.Procedure3) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) org.finos.legend.pure.generated(org.finos.legend.pure.generated) List(java.util.List) ClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) DatabaseInstance(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.DatabaseInstance) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel) InputData(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.mappingTest.InputData) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Function(org.eclipse.collections.api.block.function.Function) AuthenticationStrategy(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.authentication.AuthenticationStrategy) Lists(org.eclipse.collections.api.factory.Lists) AssociationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping) Procedure(org.eclipse.collections.api.block.procedure.Procedure) FastList(org.eclipse.collections.impl.list.mutable.FastList) DatasourceSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.specification.DatasourceSpecification) Function3(org.eclipse.collections.api.block.function.Function3) MutableMap(org.eclipse.collections.api.map.MutableMap) HelperModelBuilder.getElementFullPath(org.finos.legend.engine.language.pure.compiler.toPureGraph.HelperModelBuilder.getElementFullPath) Function2(org.eclipse.collections.api.block.function.Function2) FunctionHandlerRegistrationInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionHandlerRegistrationInfo) RelationalAssociationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RelationalAssociationMapping) Database(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database) RelationalExecutionContext(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.executionContext.RelationalExecutionContext) PureMap(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.map.PureMap) Pair(org.eclipse.collections.api.tuple.Pair) FunctionHandlerDispatchBuilderInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionHandlerDispatchBuilderInfo) ListIterate(org.eclipse.collections.impl.utility.ListIterate) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) EmbeddedRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation) Connection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.Connection) ImmutableList(org.eclipse.collections.api.list.ImmutableList) ExecutionContext(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.executionContext.ExecutionContext) AggregateSetImplementationContainer(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregateSetImplementationContainer) MapperPostProcessor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.postprocessor.MapperPostProcessor) FunctionExpressionBuilderRegistrationInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionExpressionBuilderRegistrationInfo) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) Collections(java.util.Collections) RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) AggregateSetImplementationContainer(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregateSetImplementationContainer)

Example 3 with ClassMapping

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping in project legend-engine by finos.

the class RelationalCompilerExtension method getExtraClassMappingSecondPassProcessors.

@Override
public List<Procedure3<ClassMapping, Mapping, CompileContext>> getExtraClassMappingSecondPassProcessors() {
    return Collections.singletonList((cm, parentMapping, context) -> {
        if (cm instanceof RootRelationalClassMapping) {
            RootRelationalClassMapping classMapping = (RootRelationalClassMapping) cm;
            RootRelationalInstanceSetImplementation rsi = (RootRelationalInstanceSetImplementation) parentMapping._classMappings().detect(c -> HelperRelationalBuilder.getClassMappingId(c).equals(HelperMappingBuilder.getClassMappingId(classMapping, context)));
            HelperRelationalBuilder.processRootRelationalClassMapping(rsi, classMapping, context);
        }
    });
}
Also used : PostProcessorWithParameter(org.finos.legend.pure.m3.coreinstance.meta.relational.runtime.PostProcessorWithParameter) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation) Multimap(org.eclipse.collections.api.multimap.Multimap) Schema(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Schema) RelationalAssociationImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RelationalAssociationImplementation) MutableList(org.eclipse.collections.api.list.MutableList) AggregationAwareClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregationAwareClassMapping) Maps(org.eclipse.collections.api.factory.Maps) RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) MutableSet(org.eclipse.collections.api.set.MutableSet) Handlers(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.Handlers) RichIterable(org.eclipse.collections.api.RichIterable) Store(org.finos.legend.pure.m3.coreinstance.meta.pure.store.Store) Relation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.Relation) Processor(org.finos.legend.engine.language.pure.compiler.toPureGraph.extension.Processor) Tuples(org.eclipse.collections.impl.tuple.Tuples) RelationalInputData(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.mappingTest.RelationalInputData) org.finos.legend.pure.m3.coreinstance.meta.pure.mapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping) Function4(org.finos.legend.engine.shared.core.function.Function4) IntObjectToIntFunction(org.eclipse.collections.api.block.function.primitive.IntObjectToIntFunction) PostProcessor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.postprocessor.PostProcessor) PureClientVersions(org.finos.legend.engine.protocol.pure.PureClientVersions) Objects(java.util.Objects) Procedure3(org.finos.legend.engine.shared.core.function.Procedure3) RelationalDatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.RelationalDatabaseConnection) org.finos.legend.pure.generated(org.finos.legend.pure.generated) List(java.util.List) ClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping) Procedure2(org.eclipse.collections.api.block.procedure.Procedure2) DatabaseInstance(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.DatabaseInstance) EngineException(org.finos.legend.engine.shared.core.operational.errorManagement.EngineException) org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel) InputData(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.mappingTest.InputData) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Function(org.eclipse.collections.api.block.function.Function) AuthenticationStrategy(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.authentication.AuthenticationStrategy) Lists(org.eclipse.collections.api.factory.Lists) AssociationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping) Procedure(org.eclipse.collections.api.block.procedure.Procedure) FastList(org.eclipse.collections.impl.list.mutable.FastList) DatasourceSpecification(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.specification.DatasourceSpecification) Function3(org.eclipse.collections.api.block.function.Function3) MutableMap(org.eclipse.collections.api.map.MutableMap) HelperModelBuilder.getElementFullPath(org.finos.legend.engine.language.pure.compiler.toPureGraph.HelperModelBuilder.getElementFullPath) Function2(org.eclipse.collections.api.block.function.Function2) FunctionHandlerRegistrationInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionHandlerRegistrationInfo) RelationalAssociationMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RelationalAssociationMapping) Database(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database) RelationalExecutionContext(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.executionContext.RelationalExecutionContext) PureMap(org.finos.legend.pure.runtime.java.compiled.generation.processors.support.map.PureMap) Pair(org.eclipse.collections.api.tuple.Pair) FunctionHandlerDispatchBuilderInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionHandlerDispatchBuilderInfo) ListIterate(org.eclipse.collections.impl.utility.ListIterate) AggregationAwareSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.aggregationAware.AggregationAwareSetImplementation) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) EmbeddedRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation) Connection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.Connection) ImmutableList(org.eclipse.collections.api.list.ImmutableList) ExecutionContext(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.executionContext.ExecutionContext) AggregateSetImplementationContainer(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregateSetImplementationContainer) MapperPostProcessor(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.postprocessor.MapperPostProcessor) FunctionExpressionBuilderRegistrationInfo(org.finos.legend.engine.language.pure.compiler.toPureGraph.handlers.FunctionExpressionBuilderRegistrationInfo) NamedRelation(org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.relation.NamedRelation) Collections(java.util.Collections) RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) RootRelationalInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)

Example 4 with ClassMapping

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping in project legend-engine by finos.

the class RelationalGrammarComposerExtension method getExtraClassMappingComposers.

@Override
public List<Function2<ClassMapping, PureGrammarComposerContext, String>> getExtraClassMappingComposers() {
    return Lists.mutable.with((classMapping, context) -> {
        if (classMapping instanceof RootRelationalClassMapping) {
            RelationalGrammarComposerContext ctx = RelationalGrammarComposerContext.Builder.newInstance(context).build();
            RootRelationalClassMapping rootRelationalClassMapping = (RootRelationalClassMapping) classMapping;
            StringBuilder builder = new StringBuilder();
            builder.append(": Relational\n");
            builder.append(getTabString()).append("{\n");
            if (rootRelationalClassMapping.filter != null) {
                appendTabString(builder, 2).append(HelperRelationalGrammarComposer.renderFilterMapping(rootRelationalClassMapping.filter)).append("\n");
            }
            builder.append(rootRelationalClassMapping.distinct ? (getTabString(2) + "~distinct\n") : "");
            if (!rootRelationalClassMapping.groupBy.isEmpty()) {
                appendTabString(builder, 2).append("~groupBy\n");
                appendTabString(builder, 2).append("(\n");
                builder.append(LazyIterate.collect(rootRelationalClassMapping.groupBy, op -> getTabString(3) + HelperRelationalGrammarComposer.renderRelationalOperationElement(op, ctx)).makeString(",\n"));
                builder.append("\n");
                appendTabString(builder, 2).append(")\n");
            }
            if (!rootRelationalClassMapping.primaryKey.isEmpty()) {
                appendTabString(builder, 2).append("~primaryKey\n");
                appendTabString(builder, 2).append("(\n");
                builder.append(LazyIterate.collect(rootRelationalClassMapping.primaryKey, op -> getTabString(3) + HelperRelationalGrammarComposer.renderRelationalOperationElement(op, ctx)).makeString(",\n"));
                builder.append("\n");
                appendTabString(builder, 2).append(")\n");
            }
            if (rootRelationalClassMapping.mainTable != null) {
                TablePtr tablePtr = rootRelationalClassMapping.mainTable;
                appendTabString(builder, 2).append("~mainTable ");
                builder.append("[").append(tablePtr.getDb()).append("]");
                builder.append((tablePtr.schema != null && !tablePtr.schema.equals("default")) ? (tablePtr.schema + "." + tablePtr.table) : tablePtr.table);
                builder.append("\n");
            }
            if (!rootRelationalClassMapping.propertyMappings.isEmpty()) {
                RelationalGrammarComposerContext indentedContext = RelationalGrammarComposerContext.Builder.newInstance(ctx).withIndentation(4).build();
                builder.append(LazyIterate.collect(rootRelationalClassMapping.propertyMappings, propertyMapping -> HelperRelationalGrammarComposer.renderAbstractRelationalPropertyMapping(propertyMapping, indentedContext, false)).makeString(",\n"));
                builder.append("\n");
            }
            appendTabString(builder).append("}");
            return builder.toString();
        }
        return null;
    });
}
Also used : RootRelationalClassMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping) TablePtr(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.TablePtr)

Example 5 with ClassMapping

use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping in project legend-engine by finos.

the class ClassMappingSecondPassBuilder method visit.

@Override
public SetImplementation visit(PureInstanceClassMapping classMapping) {
    PureInstanceSetImplementation s = (PureInstanceSetImplementation) parentMapping._classMappings().select(c -> c._id().equals(HelperMappingBuilder.getClassMappingId(classMapping, this.context))).getFirst();
    s._propertyMappings().forEachWithIndex((ObjectIntProcedure<org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping>) ((p, i) -> {
        org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping pm = (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p;
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property<?, ?> property = pm._property();
        SourceInformation pSourceInformation = SourceInformationHelper.fromM3SourceInformation(p.getSourceInformation());
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification last = pm._transform()._expressionSequence().getLast();
        List<Type> typesToCheck;
        if (property._genericType()._rawType()._classifierGenericType()._rawType()._name().equals("Class")) {
            SetImplementation setImplementation;
            if (p._targetSetImplementationId() != null && !p._targetSetImplementationId().equals("")) {
                setImplementation = parentMapping._classMappingByIdRecursive(Lists.fixedSize.with(p._targetSetImplementationId()), this.context.pureModel.getExecutionSupport()).getFirst();
                Assert.assertTrue(setImplementation != null, () -> "Can't find class mapping '" + p._targetSetImplementationId() + "'", pSourceInformation, EngineErrorType.COMPILATION);
            } else {
                setImplementation = parentMapping.classMappingByClass((org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class<Object>) property._genericType()._rawType(), this.context.pureModel.getExecutionSupport()).getFirst();
                Assert.assertTrue(setImplementation != null, () -> "Can't find class mapping for '" + HelperModelBuilder.getElementFullPath(property._genericType()._rawType(), this.context.pureModel.getExecutionSupport()) + "'", pSourceInformation, EngineErrorType.COMPILATION);
            }
            List<? extends InstanceSetImplementation> setImpls = core_pure_router_router_routing.Root_meta_pure_router_routing_resolveOperation_SetImplementation_MANY__Mapping_1__InstanceSetImplementation_MANY_(Lists.immutable.of(setImplementation), parentMapping, this.context.pureModel.getExecutionSupport()).toList();
            typesToCheck = setImpls.stream().map(setImpl -> ((PureInstanceSetImplementation) setImpl)._srcClass()).collect(Collectors.toList());
        } else if (((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p)._transformer() != null) {
            EnumerationMapping<Object> m = ((EnumerationMapping<Object>) ((org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PurePropertyMapping) p)._transformer());
            Object val = m._enumValueMappings().getFirst()._sourceValues().getFirst();
            if (val instanceof String) {
                typesToCheck = Collections.singletonList(this.context.pureModel.getType("String"));
            } else if (val instanceof Long) {
                typesToCheck = Collections.singletonList(this.context.pureModel.getType("Integer"));
            } else if (val instanceof EnumValue) {
                typesToCheck = Collections.singletonList(this.context.resolveEnumeration(((EnumValue) val).fullPath, pSourceInformation));
            } else if (val instanceof Enum) {
                GenericType genericType = ((Enum) val)._classifierGenericType();
                typesToCheck = genericType != null ? Collections.singletonList(this.context.resolveEnumeration(PackageableElement.getUserPathForPackageableElement(genericType._rawType()), pSourceInformation)) : Collections.emptyList();
            } else {
                typesToCheck = Collections.emptyList();
            }
        } else {
            typesToCheck = Collections.singletonList(property._genericType()._rawType());
        }
        org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity multiplicityToCheck = pm._explodeProperty() != null && pm._explodeProperty() ? this.context.pureModel.getMultiplicity("zeromany") : property._multiplicity();
        List<ValueSpecification> lines = ((PurePropertyMapping) classMapping.propertyMappings.get(i)).transform.body;
        typesToCheck.stream().filter(Objects::nonNull).forEach(t -> HelperModelBuilder.checkTypeCompatibility(this.context, last._genericType()._rawType(), t, "Error in class mapping '" + HelperModelBuilder.getElementFullPath(parentMapping, this.context.pureModel.getExecutionSupport()) + "' for property '" + pm._property()._name() + "'", lines.get(lines.size() - 1).sourceInformation));
        HelperModelBuilder.checkMultiplicityCompatibility(last._multiplicity(), multiplicityToCheck, "Error in class mapping '" + HelperModelBuilder.getElementFullPath(parentMapping, this.context.pureModel.getExecutionSupport()) + "' for property '" + pm._property()._name() + "'", lines.get(lines.size() - 1).sourceInformation);
    }));
    return s;
}
Also used : Enum(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Enum) EnumerationMapping(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.EnumerationMapping) EnumValue(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.EnumValue) ValueSpecification(org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.ValueSpecification) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) SourceInformation(org.finos.legend.engine.protocol.pure.v1.model.SourceInformation) EngineErrorType(org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) PurePropertyMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PurePropertyMapping) PurePropertyMapping(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PurePropertyMapping) InstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.InstanceSetImplementation) OperationSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.OperationSetImplementation) PureInstanceSetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.modelToModel.PureInstanceSetImplementation) SetImplementation(org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.SetImplementation)

Aggregations

EngineException (org.finos.legend.engine.shared.core.operational.errorManagement.EngineException)6 ClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.ClassMapping)5 AggregationAwareClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.aggregationAware.AggregationAwareClassMapping)4 RootRelationalClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.mapping.RootRelationalClassMapping)4 List (java.util.List)3 Objects (java.util.Objects)3 RichIterable (org.eclipse.collections.api.RichIterable)3 Pair (org.eclipse.collections.api.tuple.Pair)3 FastList (org.eclipse.collections.impl.list.mutable.FastList)3 Tuples (org.eclipse.collections.impl.tuple.Tuples)3 ListIterate (org.eclipse.collections.impl.utility.ListIterate)3 HelperModelBuilder.getElementFullPath (org.finos.legend.engine.language.pure.compiler.toPureGraph.HelperModelBuilder.getElementFullPath)3 ParseTreeWalkerSourceInformation (org.finos.legend.engine.language.pure.grammar.from.ParseTreeWalkerSourceInformation)3 MappingParserGrammar (org.finos.legend.engine.language.pure.grammar.from.antlr4.mapping.MappingParserGrammar)3 AssociationMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.mapping.AssociationMapping)3 PureInstanceClassMapping (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.modelToModel.mapping.PureInstanceClassMapping)3 Database (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.Database)3 EmbeddedRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.EmbeddedRelationalInstanceSetImplementation)3 RootRelationalInstanceSetImplementation (org.finos.legend.pure.m3.coreinstance.meta.relational.mapping.RootRelationalInstanceSetImplementation)3 org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel)3