Search in sources :

Example 1 with ClassBuilder

use of org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder in project legend-engine by finos.

the class RelationalExecutionNodeExecutor method getStreamingObjectResultFromRelationalResult.

private Result getStreamingObjectResultFromRelationalResult(ExecutionNode node, RelationalResult relationalResult, DatabaseConnection databaseConnection) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, JsonProcessingException {
    Class<?> executeClass = this.getExecuteClass(node);
    if (Arrays.asList(executeClass.getInterfaces()).contains(IRelationalClassInstantiationNodeExecutor.class)) {
        IRelationalClassInstantiationNodeExecutor executor = (IRelationalClassInstantiationNodeExecutor) executeClass.getConstructor().newInstance();
        final ResultSet resultSet = relationalResult.getResultSet();
        final String databaseTimeZone = relationalResult.getRelationalDatabaseTimeZone();
        final String databaseConnectionString = ObjectMapperFactory.getNewStandardObjectMapperWithPureProtocolExtensionSupports().writeValueAsString(databaseConnection);
        Iterator<Object> objectIterator = new Iterator<Object>() {

            private boolean cursorMove;

            private boolean hasNext;

            @Override
            public boolean hasNext() {
                if (!this.cursorMove) {
                    try {
                        this.hasNext = resultSet.next();
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    this.cursorMove = true;
                }
                return this.hasNext;
            }

            @Override
            public Object next() {
                if (this.hasNext()) {
                    cursorMove = false;
                    try {
                        return executor.getObjectFromResultSet(resultSet, databaseTimeZone, databaseConnectionString);
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                throw new NoSuchElementException("End of result set reached!");
            }
        };
        Stream<Object> objectStream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(objectIterator, Spliterator.ORDERED), false);
        ClassBuilder classBuilder = new ClassBuilder(node);
        return new StreamingObjectResult<>(objectStream, classBuilder, relationalResult);
    } else {
        throw new RuntimeException("Unknown execute class " + executeClass.getCanonicalName());
    }
}
Also used : SQLException(java.sql.SQLException) IRelationalClassInstantiationNodeExecutor(org.finos.legend.engine.plan.dependencies.store.relational.classResult.IRelationalClassInstantiationNodeExecutor) StreamingObjectResult(org.finos.legend.engine.plan.execution.result.object.StreamingObjectResult) ClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResultSet(java.sql.ResultSet) IReferencedObject(org.finos.legend.engine.plan.dependencies.store.shared.IReferencedObject)

Example 2 with ClassBuilder

use of org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder in project legend-engine by finos.

the class RelationalResult method buildTransformersAndBuilder.

private void buildTransformersAndBuilder(ExecutionNode node, DatabaseConnection databaseConnection) throws SQLException {
    boolean isDatabaseIdentifiersCaseSensitive = databaseConnection.accept(new DatabaseIdentifiersCaseSensitiveVisitor());
    if (ExecutionNodeTDSResultHelper.isResultTDS(node)) {
        List<TransformerInput<Integer>> transformerInputs = Lists.mutable.empty();
        for (int columnIndex = 1; columnIndex <= this.columnCount; columnIndex++) {
            TDSColumn c = ExecutionNodeTDSResultHelper.getTDSColumn(node, this.resultSetMetaData.getColumnLabel(columnIndex), isDatabaseIdentifiersCaseSensitive);
            transformerInputs.add(new TransformerInput<>(columnIndex, c.type, (index) -> {
                try {
                    return ExecutionNodeTDSResultHelper.isTDSColumnEnum(node, this.resultSetMetaData.getColumnLabel(index), isDatabaseIdentifiersCaseSensitive);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, (index) -> {
                try {
                    return ExecutionNodeTDSResultHelper.getTDSEnumTransformer(node, this.resultSetMetaData.getColumnLabel(index), isDatabaseIdentifiersCaseSensitive);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }));
        }
        setTransformers.add(new SetImplTransformers(transformerInputs));
        this.builder = new TDSBuilder(node, this.sqlColumns, isDatabaseIdentifiersCaseSensitive);
        this.columnListForSerializer = ListIterate.collect(((TDSBuilder) this.builder).columns, col -> col.name);
    } else if (ExecutionNodeClassResultHelper.isClassResult(node)) {
        List<? extends ClassMappingInfo> classMappings = ExecutionNodeClassResultHelper.getClassMappingInfoFromClassResult(node);
        for (ClassMappingInfo classMappingInfo : classMappings) {
            List<TransformerInput<String>> transformerInputs = Lists.mutable.empty();
            for (int i = 1; i <= this.columnCount; i++) {
                final String colName = this.resultSetMetaData.getColumnLabel(i);
                PropertyInfo profiles = ListIterate.select(classMappingInfo.properties, p -> isDatabaseIdentifiersCaseSensitive ? p.property.equals(colName) : p.property.equalsIgnoreCase(colName)).getFirst();
                transformerInputs.add(new TransformerInput<>(profiles != null ? profiles.property : colName, resolveType(profiles, colName), (colNameX) -> {
                    try {
                        return !TEMPORAL_DATE_ALIASES.contains(colNameX) && ExecutionNodeClassResultHelper.isClassPropertyEnum(node, classMappingInfo.setImplementationId, colNameX);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }, (colNameX) -> {
                    try {
                        return ExecutionNodeClassResultHelper.getClassEnumTransformer(node, classMappingInfo.setImplementationId, colNameX);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }));
            }
            setTransformers.add(new SetImplTransformers(transformerInputs));
            if (ExecutionNodePartialClassResultHelper.isPartialClassResult(node)) {
                this.builder = new PartialClassBuilder(node);
            } else {
                this.builder = new ClassBuilder(node);
            }
        }
    } else if (ExecutionNodeRelationalResultHelper.isRelationResult(node)) {
        SetImplTransformers setImpl = new SetImplTransformers();
        for (int columnIndex = 1; columnIndex <= this.columnCount; columnIndex++) {
            setImpl.transformers.add(SetImplTransformers.TEMPORARY_DATATYPE_TRANSFORMER);
        }
        setTransformers.add(setImpl);
        this.builder = new RelationBuilder(node);
    } else {
        SetImplTransformers setImpl = new SetImplTransformers();
        for (int i = 1; i <= this.columnCount; i++) {
            setImpl.transformers.add(SetImplTransformers.TEMPORARY_DATATYPE_TRANSFORMER);
        }
        setTransformers.add(setImpl);
        this.builder = new DataTypeBuilder(node);
    }
}
Also used : LoggingEventType(org.finos.legend.engine.shared.core.operational.logs.LoggingEventType) DatabaseManager(org.finos.legend.engine.plan.execution.stores.relational.connection.driver.DatabaseManager) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer) DatabaseType(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.DatabaseType) Connection(java.sql.Connection) ClassMappingInfo(org.finos.legend.engine.plan.execution.result.builder._class.ClassMappingInfo) ExecutionNodeTDSResultHelper(org.finos.legend.engine.plan.execution.nodes.helpers.ExecutionNodeTDSResultHelper) SerializationFormat(org.finos.legend.engine.plan.execution.result.serialization.SerializationFormat) RelationalInstantiationExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.RelationalInstantiationExecutionNode) TransformerInput(org.finos.legend.engine.plan.execution.result.transformer.TransformerInput) MutableList(org.eclipse.collections.api.list.MutableList) TDSColumn(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.result.TDSColumn) ResultSet(java.sql.ResultSet) ExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.ExecutionNode) RelationBuilder(org.finos.legend.engine.plan.execution.stores.relational.result.builder.relation.RelationBuilder) Builder(org.finos.legend.engine.plan.execution.result.builder.Builder) IRelationalResult(org.finos.legend.engine.plan.dependencies.store.relational.IRelationalResult) SQLResultColumn(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.model.result.SQLResultColumn) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) Timestamp(java.sql.Timestamp) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) LogInfo(org.finos.legend.engine.shared.core.operational.logs.LogInfo) RelationalResultToPureTDSSerializer(org.finos.legend.engine.plan.execution.stores.relational.serialization.RelationalResultToPureTDSSerializer) List(java.util.List) DatabaseConnection(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.store.relational.connection.DatabaseConnection) Lists(org.eclipse.collections.impl.factory.Lists) PureDate(org.finos.legend.engine.plan.dependencies.domain.date.PureDate) ExecutionNodeClassResultHelper(org.finos.legend.engine.plan.execution.nodes.helpers.ExecutionNodeClassResultHelper) Span(io.opentracing.Span) PropertyInfo(org.finos.legend.engine.plan.execution.result.builder._class.PropertyInfo) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) CommonProfile(org.pac4j.core.profile.CommonProfile) RelationalExecutionNode(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.nodes.RelationalExecutionNode) Function(org.eclipse.collections.api.block.function.Function) ExecutionActivity(org.finos.legend.engine.plan.execution.result.ExecutionActivity) PartialClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.PartialClassBuilder) SQLException(java.sql.SQLException) RelationalResultToJsonDefaultSerializer(org.finos.legend.engine.plan.execution.stores.relational.serialization.RelationalResultToJsonDefaultSerializer) RelationalExecutionActivity(org.finos.legend.engine.plan.execution.stores.relational.activity.RelationalExecutionActivity) ErrorResult(org.finos.legend.engine.plan.execution.result.ErrorResult) Result(org.finos.legend.engine.plan.execution.result.Result) ResultVisitor(org.finos.legend.engine.plan.execution.result.ResultVisitor) Logger(org.slf4j.Logger) ListIterate(org.eclipse.collections.impl.utility.ListIterate) ClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder) SetImplTransformers(org.finos.legend.engine.plan.execution.result.transformer.SetImplTransformers) DataTypeBuilder(org.finos.legend.engine.plan.execution.result.builder.datatype.DataTypeBuilder) ExecutionNodePartialClassResultHelper(org.finos.legend.engine.plan.execution.nodes.helpers.ExecutionNodePartialClassResultHelper) Consumer(java.util.function.Consumer) RelationalResultToCSVSerializer(org.finos.legend.engine.plan.execution.stores.relational.serialization.RelationalResultToCSVSerializer) ImmutableList(org.eclipse.collections.api.list.ImmutableList) Statement(java.sql.Statement) RelationalResultToPureTDSToObjectSerializer(org.finos.legend.engine.plan.execution.stores.relational.serialization.RelationalResultToPureTDSToObjectSerializer) TDSBuilder(org.finos.legend.engine.plan.execution.result.builder.tds.TDSBuilder) PartialClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.PartialClassBuilder) SetImplTransformers(org.finos.legend.engine.plan.execution.result.transformer.SetImplTransformers) PartialClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.PartialClassBuilder) ClassBuilder(org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder) TransformerInput(org.finos.legend.engine.plan.execution.result.transformer.TransformerInput) SQLException(java.sql.SQLException) ClassMappingInfo(org.finos.legend.engine.plan.execution.result.builder._class.ClassMappingInfo) DataTypeBuilder(org.finos.legend.engine.plan.execution.result.builder.datatype.DataTypeBuilder) TDSColumn(org.finos.legend.engine.protocol.pure.v1.model.executionPlan.result.TDSColumn) RelationBuilder(org.finos.legend.engine.plan.execution.stores.relational.result.builder.relation.RelationBuilder) MutableList(org.eclipse.collections.api.list.MutableList) List(java.util.List) ImmutableList(org.eclipse.collections.api.list.ImmutableList) TDSBuilder(org.finos.legend.engine.plan.execution.result.builder.tds.TDSBuilder) PropertyInfo(org.finos.legend.engine.plan.execution.result.builder._class.PropertyInfo)

Aggregations

ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ClassBuilder (org.finos.legend.engine.plan.execution.result.builder._class.ClassBuilder)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Span (io.opentracing.Span)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Connection (java.sql.Connection)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 Types (java.sql.Types)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1 Consumer (java.util.function.Consumer)1 Function (org.eclipse.collections.api.block.function.Function)1 ImmutableList (org.eclipse.collections.api.list.ImmutableList)1 MutableList (org.eclipse.collections.api.list.MutableList)1 Lists (org.eclipse.collections.impl.factory.Lists)1 ListIterate (org.eclipse.collections.impl.utility.ListIterate)1